The operator & extracts the memory address of a certain variable, which can be used for a wide variety of things.
Examples:
Suppose you want to create a function that adds an integer x to an integer variable y. What many laymen and beginners would do is:
#include <stdio.h>
void adicionar( int x, int y){
y += x;
}
int main(){
int a = 5, b = 2;
printf("Valor inicial de b: %i\n", b);
adicionar(a, b);
printf("Valor final de b: %i", b);
return 0;
}
Output:
Valor inicial de b: 2
Valor final de b: 2
Yeah, but we don’t ask the function adicionar
that the value of variable b is modified?
Unfortunately that’s not how things work: When we pass b to function adicionar
, we spent a copy from b to function. Why does this happen? Because the function requested the value contained in b, and not its memory address. Thus, it is impossible to modify the original variable b.
We can think of this situation as follows: In function adicionar
:
void adicionar( int x, int y){
int x = x;
int y = y;
}
That is, when a function requires a valor
of a variable, it’s as if internally it recreates the variables that we’ve gone through, only in its own scope. Thus, the variables that we pass as an argument are ignored, and the variables of the function scope are used.
To solve this unfortunate problem, an alternative would be to modify the add function as follows:
void adicionar( int x, int *y){
*y += x;
}
Here, we ask for a pointer for integer y. This basically means that we are requesting a memory address for a variable, that is, a memory location that we will modify within the function. The code snippet *y += x
basically makes the dereference of the y pointer, and adds to that dereference the value x. For ease of understanding, we could write the following pseudo-function code:
void adicionar( cópia de inteiro x, ponteiro para inteiro y){
Desreferencie, ou seja, obtenha o endereço de memória para o qual y aponta.
Em seguida, some a cópia do valor de x ao ponteiro recentemente desreferenciado.
}
In that case, we can do the following in our role main()
:
int main(){
int a = 5, b = 2;
printf("Valor inicial de b: %i\n", b);
adicionar( a, &b);
printf("Valor final de b: %i\n", b);
return 0;
}
Output:
Valor inicial de b: 2
Valor final de b: 7
But what is this one &
which we entered before b when calling the add function? What does it do? Very simple: It passes a pointer to a memory address! It is as if we are implicitly starting the parameter y of the prototype of our function with the address of b! Thus, when we deviate y within the function adicionar
, we are making sure that the address of b, to which y is pointing, has its value modified!
Therefore, it is concluded that &
is an operator that allows us to access and modify a memory address of a variable.
We could also modify the contents of b without the use of functions using pointers indirect coupled to the use of the operator &
. For example:
#include <stdio.h>
int main(){
int a = 5, b = 2, *c = NULL;
c = &b; //c agora aponta para o endereço de b, obtido com o operador &.
*c += a; //O valor contido no endereço para qual c aponta (b) é igual a ele mesmo mais a.
printf("O valor de b é: %i", b);
return 0;
}
Output:
O valor de b é: 7
Another use of &
, less known to beginners, is for carrying out the logical operation AND. For example:
if((24 & 50) > 30){
puts("(24 & 50) é maior que 30.");
}
else{
puts("(24 & 50) é igual ou menor que 30.");
}
Knowing that &
represents, in this case, the logical operation AND, obtained:
0 0 0 1 1 0 0 0
0 0 1 1 0 0 1 0
____________________________________
0 0 0 1 0 0 0 0
As the result obtained is 16 on the decimal basis, the output of this conditional structure is:
(24 & 50) é igual ou menor que 30.
In the C language we have two ways to pass parameters through values and by reference & that used this operator.
– Eduardo Sampaio
Possible duplicate of What is the difference between pointer and reference?
– Francisco