Chapter 9. Pointers
A pointer is a reference to a data object or a function. Pointers have many uses, such as defining “call-by-reference” functions and implementing dynamic data structures such as linked lists and trees, to name just two examples.
Very often the only efficient way to manage large volumes of data is to manipulate not the data itself but pointers to the data. For example, if you need to sort a large number of large records, it is often more efficient to sort a list of pointers to the records, rather than moving the records themselves around in memory. Similarly, if you need to pass a large record to a function, it’s more economical to pass a pointer to the record than to pass the record contents, even if the function doesn’t modify the contents.
Declaring Pointers
A pointer represents both the address and the type of an object or function. If an object or function has the type T
, then a pointer to it has the derived type pointer to T
. For example, if var
is a float
variable, then the expression &var
—whose value is the address of the float
variable—has the type pointer to float
, or in C notation, the type float *
. A pointer to any type T
is also called a T
pointer for short. Thus, the address operator in &var
yields a float
pointer.
Because var
doesn’t move around in memory, the expression &var
is a constant pointer. However, C also allows you to define variables with pointer types. A pointer variable stores the address of another object or a function. We describe ...
Get C in a Nutshell, 2nd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.