Lesson 11.2. Syntax of C pointers

The ampersand « & »

You probably don't know this, but you have most likely already used pointers:

scanf("%d", &n);

The & (and commercial or ampersand) in front of a variable means that we use its address. The scanf() function receives the address of the variable, which can modify its content to write the user's input.

When you forget the & in a scanf(), the function takes the value of the variable as its address. If the variable contains 0, the function will try to write in the first byte of the memory, which usually leads to a segmentation error.

The example below displays the address of the variable x:

var x;
printf ("%p", &x);

Declaring a pointer

It is possible to declare a pointer, i.e. a variable intended to contain the address of another variable. You just have to add an asterisk in front of the name of the pointer:

int *address1;
char *address2; 

A pointer is always associated with a type. Since the pointer contains only the address of the first byte, the type designation allows the compiler to know the size of the pointed variable. In the following example, the pointer address contains the address of the variable var. We say that var is pointed to by the address pointer or that address points to var :

float var;
float *address = &var;

Access to the pointed variable

The interest of pointers is to be able to access a variable in reading and/or writing by its address. The access to the pointed content is done by adding an asterisk before the pointer :

float var;
float *address = &var;
*address = 5.2;  // Equivalent to var=5.2;

Example

Here are some example of pointer syntaxes:

// Declare a variable
short var=0xABCD;
// Declare a pointer on var
short* address = &var; 

// Display var
printf ("var      : %hX\n", var);

// Displat the value pointed by address (= var)
printf ("*address : %hX\n", *address);

// Display the address of var
printf ("&var     : %p\n", &var);

// Display the pointer address (adress of var)
printf ("address  : %p\n", address);

// Display the pointer address
printf ("&address : %p\n", &address);

Here is a possible output for this program:

var      : ABCD
*address : ABCD
&var     : 0x7ffcda51423a
address  : 0x7ffcda51423a
&address : 0x7ffcda514230

The following image illustrates the representation of the memory for this execution of the program. We can see the two main entities:

We can see that the content of the pointer is indeed the address of var. The pointer address points to the variable var :

variable and pointer stores in memory

Exercises

Exercise 1

Write a program that

var = 18
*address = 18

Exercise 2

Write a program that

We can see that the pointer has not been increased by one byte, but by 4 bytes, which corresponds to the size of an integer :

Before : 0x7ffe4b0a7e88
After  : 0x7ffe4b0a7e8c

Quiz

How to get the address of a variable?

Check Bravo! The ampersand gives the address of the variable. Try again...

What syntaxes are used to declare a pointer?

Check Bravo! int* or int * declare a pointer intended to contain the address of an integer Try again...

How to display the content of the variable pointed by the pointer ad?

Check Bravo! *ad is the value pointer by ad Try again...

How to display the address contained in the ad pointer?

Check Bravo! ad contains an address displayed with %p format code. Try again...

How to display the address of the ad pointer?

Check Bravo! &ad is the address of the pointer himself. Try again...

What does the following program display?

int var = 128, *address;
address = &var;
printf ("%d %d", var, *address);
Check Bravo! As the pointer points to the variable, the content of the variable is displayed twice. Try again...

What does the following program display?

int var = 128, *address;
address = &var;
printf ("%p", address);
Check Bravo! The memory addresses allocated to the variables can change at each execution. Try again...

A pointer is associated with a type ...

Check Bravo! The compiler must know the size of the pointed variables to guarantee the consistency of the operations. Try again...

See also


Last update : 11/29/2022