Why do different types of variables give different results when modified in a function?

Asked

Viewed 53 times

2

I can’t understand why x and v[0] are different.

void edit1 (int x) 
{
   x = 9 * x;
}


void edit2 (int v[]) 
{
   v[0] = 9 * v[0];
}


int main () 
{
   int x, v[2];

   x = 678;
   edit1 (x); 
printf (" x: %d\n" , x);

   v[0] = 678;
   edit2 (v); 
printf (" v[0]: %d\n" , v[0]);

   return 0;
}

1 answer

1


In one of the functions what is going through is a value that is in the variable, in the other is passing a pointer indicating where is a value that matters. In the first who is copied to the parameter is the value that matters. In the second what is copied to the parameter is the pointer.

When you change the value of the parameter, you are changing only that, the parameter, the variable used as argument has nothing to do with the parameter, and is not affected, are different variables, even if it has the same name, the scope is another, that’s why there are functions, you isolate the parts.

When the parameter is a pointer, and understand that the array is a pointer, the same happens if you change the pointer nothing is affected in the variable used as argument.

But what its function does is not change the pointer, it changes a value that is inside an object pointed by that pointer.

When you create a pointer, and I’ll repeat, a array is just an address that points to a memory position that will possibly have a sequence of values, you say where your values are, the most common is not to store these values in a variable, but rather you keep the pointer that tells where the values are stored. This is called indirect.

So when you change an element of array is changing a memory position where the data from array. Nothing is copied in this part, only the pointer is, you have two different variables in different functions, even if it is of the same name, I have explained this, that point to the same object, the same sequence of values. If you change a value in this sequence, any pointer referencing to that object will see the change, because it is the same object.

So in this entire application you have the following objects:

`x` de `main()`
`x` de `func1()`
`v` de `main()`
`v` de `func2()`
o objeto que é uma sequência de dados que é apontado por duas variáveis diferentes

Not all these objects exist at the same time. The object that is the sequence and variables of main() exist throughout the application because it is the input function so everything that is created there will only be destroyed at the end of the application. Variables from other s[ó functions exist while the function is running, there is no variable before running or after it has already been run.

When does int v[] is the same as doing int *v.

Commented code:

#include <stdio.h>

void func1(int x) { //o valor do argumento é copiado para x
   printf("endereço de x: %p\n", (void *)&x); //peguei o endereço de x
   x = 9 * x; //mudou o parâmetro e logo em seguida é descartado
   printf("endereço de x: %p\n", (void *)&x); //peguei o endereço de x
}

void func2(int v[]) { //o valor do argumento é copiado para x, no caso é o valor do poneiro
   printf("endereço de v: %p\n", (void *)v); //v já é um endereço
   v[0] = 9 * v[0]; //o valor apontado para o primeiro elemento no objeto é mudado
   printf("endereço de v: %p\n", (void *)v); //v já é um endereço
}

int main () {
   int x = 111, v[2]; //x tem um valor direto na variável x, v tem um ponteiro para outro objeto
   func1(x); //111 é passado para a função, só isso, nada a ver com x
   printf("endereço de x: %p\n", (void *)&x); //peguei o endereço de x
   printf("x: %d\n", x); //x aqui continua sem ser alterado
   printf("endereço de x: %p\n", (void *)&x); //peguei o endereço de x
   printf("endereço de v: %p\n", (void *)v); //v já é um endereço
   v[0] = 111; //colocou um valor lá no objeto criado para ser o array
   printf("endereço de v: %p\n", (void *)v); //v já é um endereço
   func2(v); //o endereço da memória onde foi colocado o objeto array é passado para a função
   printf("endereço de v: %p\n", (void *)v); //v já é um endereço
   printf("v[0]: %d\n", v[0]); //acessando um dos elementos ele foi alterado
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.