What is the meaning of the "&" (and commercial) operator in the C language?

16

I'm putting together a C booklet and I'm looking for a clear way to explain this operator to the reader, and I believe that this doubt will greatly help the people who are just getting started.

See an example:

Code

#include <stdio.h>
#include <stdlib.h>

int main ()
{
    int   idade;
    printf("Digite sua idade ");
    scanf("%d", &idade); 
    system ("pause");
    return(0);
}

So I understood %d and a specifier of format which can be used to read or display a variable of type inteiro .

What is the meaning of & operator?

    
asked by anonymous 28.04.2016 / 18:53

2 answers

25

In this context is the "address" operator. Then the result of it will always be the memory address of the object in question (usually the location where a variable is allocated in memory). That is, it creates a pointer.

This is C's way to pass an argument to a function. In C this passage always has to be explicit, since all passages are given by value (in general the languages are like this, but some hide this and it seems that they are passing by reference).

In the specific case, scanf() expects an address where it should store what the user types. So it's being passed with this operator.

For this function it does not matter the value stored in the variable but rather where it is.

A common mistake is the inexperienced programmer using this operator in a variable that is is a pointer. It does not need to, because the variable already contains the address of the pointed object.

The code in question can be read as: "read data in integer decimal form and store in the old address ".

The opposite operator is the * which is to get the value pointed to by the address.

There is a operator & used in another context that does a and calculation in bits .

In C ++ it is also used as another operator or declarer to indicate reference (which exists only concretely in this language and not in C) which is similar, but a bit different from pointer.

28.04.2016 / 19:03
4

The & extracts the memory address of a particular 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 would be:

#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

Yes, but we do not ask the function adicionar to change the value of variable b? Unfortunately this is not how things work: When we pass b to the function adicionar , we pass a copy from b to the function. Why does it happen? Because the function called for the value contained in b, not its address in memory . In this way, it is impossible to change 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 requests a valor of a variable, it is as if it internally recreates the variables that we pass, only in a proper scope. In this way, the variables we pass as arguments are ignored, and the function scope variables 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 from a variable, that is, a location in memory that we will modify within the function. The *y += x snippet basically causes the dereference of the y pointer, and adds the x value to that dereference. 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 this case, we can do the following in our function 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 & that we enter before b by calling the add function? What he does? Very simple: It passes a pointer to a memory address! It's as if we were implicitly starting the y-parameter of our function with the address of b! In this way, when we dereferentiate y within the adicionar function, we are causing the address of b, for which y is pointing, to have its modified value!

Therefore, we conclude 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 indirection coupled with 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 by beginners, is to perform the logical AND operation. 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 AND operation, we obtain:

0    0    0    1    1    0    0    0
0    0    1    1    0    0    1    0
____________________________________
0    0    0    1    0    0    0    0

Since the result is 16 in the decimal base, the output of this conditional structure is:

(24 & 50) é igual ou menor que 30.
    
23.04.2017 / 21:10