Lesson 11.1. Introduction to pointers in C


In C, a pointer is a special variable that contains a memory address, usually the address of another variable, array or function. Pointers have several advantages:

RAM (Random Access Memory)

RAM stands for Random Access Memory. It is obviously not a matter of having uncertain access to memory, but of being able to access any section of memory directly. A better translation would be direct access memory as opposed to sequential access memory.

A sequential access memory requires to browse from the beginning of the memory to the desired element. The typical example is tape recording:

Memory on magnetic tape

To provide direct access to the RAM, the technical solution is to associate an address to each byte of the memory. The addresses are generally represented in their hexadecimal form.


Consider the following code which displays the addresses of variables i, f and s :

int i=0;
float f;
char s[]="Hello";

printf ("Addresse of s: %p\n", &s);
printf ("Addresse of f: %p\n", &f);
printf ("Addresse of i: %p\n", &i);

Note in passing that the format code %p is for displaying a pointer.

Here is an illustration of the memory allocation for this program (note that each execution can produce different addresses):

Address of s: 0x7fff57edbf5e
Address of f: 0x7fff57edbf64
Address of i: 0x7fff57edbf68

At the time of the execution of the program (and not during the compilation), a memory space is allocated for each variable. This memory space will consist of one or more bytes. Each variable will be associated with an address that will allow direct access. Here is a representation of the memory :

Memory allocation and pointer representation in C

We distinguish the allocation of each variable in the memory. The addresses in bold represent the base address (i.e. the address of the pointer).

In this example the variables are grouped together. This strategy (which is not systematic) allows to save memory. An analogy would be the minimization of waste in the cutting of a fabric.


Exercise 1

Using the example above, write a program that declares an array of 8 characters and displays the address of each cell in the array:

Address of t[0]: 0x7ffeb0c9bc60
Address of t[1]: 0x7ffeb0c9bc61
Address of t[2]: 0x7ffeb0c9bc62
Address of t[3]: 0x7ffeb0c9bc63
Address of t[4]: 0x7ffeb0c9bc64
Address of t[5]: 0x7ffeb0c9bc65
Address of t[6]: 0x7ffeb0c9bc66
Address of t[7]: 0x7ffeb0c9bc67

Exercise 2

Using the previous example, write a program that declares an array of 8 variables of type double and displays the address of each cell. The cells of the array must be consecutive, but separated by 8 bytes:

Address of t[0] : 0x7ffed37d4f70
Address of t[1] : 0x7ffed37d4f78
Address of t[2] : 0x7ffed37d4f80
Address of t[3] : 0x7ffed37d4f88
Address of t[4] : 0x7ffed37d4f90
Address of t[5] : 0x7ffed37d4f98
Address of t[6] : 0x7ffed37d4fa0
Address of t[7] : 0x7ffed37d4fa8


In computer science, what does RAM stand for?

Check Bravo! Random-access memory (RAM) is a form of computer memory that can be read and changed in any order. Try again...

In computer science, what can be a pointer?

Check Bravo! Generally speaking, a pointer is the memory address of another element. Try again...

When is the memory allocated?

Check Bravo! Memory is allocated when variables are declared, whether they are local or global. Try again...

See also

Last update : 11/29/2022