C PointersThe only peculiarity of C is how heavily it relies on the use of pointers, compared with other languages, and the relatively permissive view of what you can do with them. See the C Book for a (much) more comprehensive discussion on pointers. Pointer ArithmeticThis section is easier illustrated by example: You may expect the two items printed to be equal, but they're not; on a system where long is 4 bytes long, here's what you'd get: The += and -= operators can involve pointers as long as the left-hand side is a pointer to an object and the right-hand side is an integral expression. The arithmetic rules above describe what happens. The ptrdiff_t TypeTwo pointers to compatible types whether or not qualified may be subtracted. The result has the type ptrdiff_t, which is defined in the header file <stddef.h>. Both pointers must point into the same array, or one past the end of the array, otherwise the behaviour is undefined. The value of the result is the number of array elements that separate the two pointers; for example: Pointers, Incrementing, and Decrementing
Untyped PointersIn certain cases it's essential to be able to convert pointers from one type to another. This is always done with the aid of casts, in expressions like the one below: The expression is converted into "pointer to type", regardless of the expression's previous type. This is only supposed to be done if you're sure that you know what you're trying to do. It is not a good idea to do much of it until you have got plenty of experience.
Generic Pointers (void *) and mallocThere are also some occasions when you will want to use a "generic" pointer. The most common example is the malloc library function, which is used to allocate storage for objects that haven't been declared. It is used by telling it how much storage is wanted—enough for a float, or an array of int, or whatever. It passes back a pointer to enough storage, which it allocates in its own mysterious way from a pool of free storage (the way that it does this is its own business). That pointer is then cast into the right type — for example if a float needs 4 bytes of free store, this is the flavour of what you would write: Malloc finds 4 bytes of store, then the address of that piece of storage is cast into pointer-to-float and assigned to the pointer. What type should malloc be declared to have? The type must be able to represent every known value of every type of pointer; there is no guarantee that any of the basic types in C can hold such a value. The solution is to use the void * type, and in fact this is how
Pointers to void can be freely converted backwards and forwards with Example
Pointer ComparisonYou can only compare:
It does not matter if the types that are pointed to are qualified or unqualified. ArraysArrays are simple and unsurprising, except for the fact that when it's used in an expression, an array name usually converts into a pointer to its first element; that often takes time to sink in.
|
|||||||||||||||||||||||

Add Comment