pointers

pointers, what are they and what does memory look like?

Pointers store addresses of variables. You can think of memory as a long table with 2 columns.

address opcode # explaination (not found in memory)
01 “hi” # variable at address 01 storing “hi”
02 21 # variable at address 02 storing 21
02 address 02 # this is a pointer, address 02

Pointer variables store the address of other variables, which can then be accessed, modified, freed etc.

Because the range of values a pointer can take differs from the range of values an integer can take, we need the new kind of variable, pointer variables.

To declare a pointer variable, we have to precede the name with an asterisk

int *p; // points to objects of type int

In the above, int is the referenced type.

int i, *p = &i; // legal

What does the address operator do?

* ~= &−1 see: [[operator___20240603_115338#address operator|address operator]]

int i, j, *p, *q;

p = &i;

q = p; // both q and p point to i.
*q = *p; // the value that p points to is copied to the object that q points to.

uninitialised pointers

int *p;
printf("%d", *p); // undefined behaviour

*p = 1 // attempts to modify an abitrary mem address

difference between q = p and *q = *p

pointers as arguments in a function

void
f(const int *p)
{
    *p = 0; /* compiler will throw error */
}

what is the difference between an address and a pointer?

Consider a computer whose memory is divided into words rather than bytes. For sake of argument, suppose a word has 36 bits. When memory is divided into words, each word has an address. But a variable doesn’t fall perfectly into 1 word all the time. e.g. a character might be 9 bits. But the character needs a pointer, which may hold an address and the offset.

pointers as return values

int *max(int *a, int *b)
{
if (*a > *b)
    return a;
else
    return b;
}

max can return a pointer passed into it, but also a pointer to an external variable or to a local variable that is static.

Pitfall

Never return a pointer to an automatic variable

int *f(void)
{
    int i;
    return &i; // i doesn't exist when f returns, so the pointer will be invalid
}

c

see also link_between_pointers_and_array

related

standard

gold

backlinks