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);
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;
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;
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:
var
variableaddress
pointerWe can see that the content of the pointer is indeed the address of var
. The pointer address
points to the variable var
:
Write a program that
var
and initializes it to 18 ;address
;var
to the pointer ;var
address
(*address
).var = 18 *address = 18
Write a program that
var
and initializes it to 27 ;address
;var
to the pointer ;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
How to get the address of a variable?
What syntaxes are used to declare a pointer?
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
?
*ad
is the value pointer by ad
Try again...
How to display the address contained in the ad
pointer?
ad
contains an address displayed with %p
format code.
Try again...
How to display the address of the ad
pointer?
&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);
What does the following program display?
int var = 128, *address;
address = &var;
printf ("%p", address);
A pointer is associated with a type ...