<< Chapter < Page | Chapter >> Page > |
char c = 'A', *cPtr =&c, **cPtrPtr =&cPtr;
The expression *cPtrPtr now yields the char pointer cPtr, and the value of **cPtrPtr is the char variable c. The diagram in Figure X illustrates these references.
There are times when it’s necessary to have a pointer that doesn’t point to anything. A null pointer is what results when you convert a null pointer constant to a pointer type. A null pointer constant is an integer constant expression with the value 0, or such an expression cast as the type void *.
Null pointers are implicitly converted to other pointer types as necessary for assignment operations, or for comparisons using == or !=. Hence no cast operator is necessary in the previous example.
A pointer to void, or void pointer for short, is a pointer with the type void *. As there are no objects with the type void, the type void * is used as the all-purpose pointer type. In other words, a void pointer can represent the address of any object but not its type. To access an object in memory, you must always convert a void pointer into an appropriate object pointer.
An array contains objects of a given type, stored consecutively in a continuous memory block.The individual objects are called the elements of an array. The elements' type can be any object type. No other types are permissible: array elements may not have a function type or an incomplete type.
An array is also an object itself, and its type is derived from its elements' type. More specifically, an array's type is determined by the type and number of elements in the array. If an array's elements have type T, then the array is called an "array of T." If the elements have type int, for example, then the array's type is "array of int." The type is an incomplete type, however, unless it also specifies the number of elements. If an array of int has 16 elements, then it has a complete object type, which is "array of 16 int elements."
In C, there is a strong relationship between pointers and arrays, strong enough that pointers and arrays should be discussed simultaneously. Any operation that can be achieved by array subscripting can also be done with pointers. The pointer version will in general be faster but, at least to the uninitiated, somewhat harder to understand.
The definition of an array determines its name, the type of its elements, and the number of elements in the array. The general syntax for declaring a single-dimensional array is
type name[ number_of_elements ];
The number of elements, between square brackets ([ ]), must be an integer expression whose value is greater than zero.
For example, the declaration,
int a[10];
defines an array of size 10, that is, a block of 10 consecutive objects named a[0], a[1], ...,a[9].
char buffer[4*512];
defines an array with the name buffer, which consists of 2,048 elements of type char.
Notification Switch
Would you like to follow the 'Introduction to computer science' conversation and receive update notifications?