Pointer Types
A pointer is an object containing the address of a storage location. A pointer can reference or "point to" any function or object, including another pointer. You declare a pointer by including an asterisk (* ) within the declarator. The following examples are declarations of pointers.
int *int_ptr;
char **str_ptr;
double (*func_ptr)(void);
The preceding examples declare the following:
int_ptr is a pointer to an int .
str_ptr is a pointer to a pointer to a char .
func_ptr is a pointer to a function that has no parameters and returns a value of the type double .
These pointers are all pointer variables. A pointer variable is a pointer that is a modifiable lvalue. Therefore, you can assign various values to it. In VOS C, pointers of all types are four bytes long, have the same format, and store an unsigned address.
Every pointer (except a pointer to void ) has an associated data type, that of the function or object to which it points. Mixing pointer types is a common source of error. The type associated with a pointer and the type of an object whose address is assigned to the pointer must be the same. In the preceding examples, int_ptr has the type "pointer to int ." Therefore, only the address of an object of the type int should be assigned to this pointer. See the "Pointer Operations" section that follows for more information on using pointers.
The explanation of pointers is divided as follows:
- pointer operations
- pointer arithmetic
- null pointer constants.
The sections that follow do not contain information on function pointers or on pointers to void . See the "Function Pointers" section in Chapter 6 for information on function pointers. See the "Void Type" section later in this chapter for information on pointers to void .
|