Pointers represent the address of a location in memory, serving as the connections that hold the pieces of linked structures together. A variable storing a pointer to a given data item can provide more freedom than storing a copy of the item itself.
Analogy: A cell-phone number can be thought of as a pointer to its owner as they move about the planet.
Syntax
In C/C++ syntax:
- A pointer
p
is assumed to give the address in memory where a particular chunk of data is located. - We use
*p
to denote the item that is pointed to by pointerp
– de-reference. - We use
&x
to denote the address of (i.e. pointer to) a particular variablex
. - A special
NULL
pointer value is used to denote structure-terminating or unassigned pointers.
Example
int myAge = 23; // An int variable
int* ptr = &myAge; // A pointer variable, with the name ptr, that stores the address of myAge
// Output the value of myAge (23)
printf("%d\n", myAge);
// Output the memory address of myAge (0x00220022)
printf("%p\n", &myAge);
// Output the memory address of myAge using the pointer (0x00220022)
printf("%p\n", ptr);
// Dereference: Output the value of myAge using the pointer (23)
printf("%d\n", *ptr);
Structs
The ->
operator in C is specifically used for accessing members of a struct through a pointer. When we have a pointer to a struct, we use the ->
operator to access the fields of the struct that the pointer points to.
struct example {
int a;
};
struct example e;
e.a = 5;
struct example *p = &e;
// Returns 5
printf(p->a); // This would return 5, and is equivalent to (*p).a or e.a