Trying to understand pointers in c / c++

Hi guys I'm new to stack and it's as follows: I am studying pointers and did the following code:

typedef struct {
    char nom[256];
    int size;
    float value;
}tdados; // criando o tipo tdados com base na struct

// função que inicializa

tdados inicializa(tdados *dados) { // iremos apontar para uma variabvel
(*dados).size = -1;
}

tdados e_inicializa(tdados dados) { // por não apontar para o endereço dá erro
    dados.size = 2;
    // o erro é que em vez de ir por endereço fdaz um fetch da variavel que causa problemas
    // o programa nã realiza o fetch
}
int main(int argc, char const *argv[])
{
    tdados dados;
    tdados edados;
    inicializa(&dados);
    e_inicializa(edados);


    fprintf(stdout,"Inicialização correta\nTamanho:\t%i\n",dados.size);
    fprintf(stdout,"Inicialização errada\nTamanho:\t%i\n",edados.size);
    return 0;
}

I know that in the case of initialize function it works because I use the Pointer of the data variable to be able to work with this and at the time of the call I pass the absolute value ( & ) contained in the address, in the second case (function e_initialize) I just pass the variable in a "simple"way. I know I'm using pointer but would like the justification of why a case it works and another does not.

Author: vfbraton, 2019-03-19

2 answers

Because when you don't pass by pointer or reference, it passes as copy.

That is, changes a new object created at a new address.

With this example, it is easier to illustrate:

#include <stdio.h>

typedef struct
{
    char nom[256];
    int size;
    float value;
} tdados; // criando o tipo tdados com base na struct

// função que inicializa

tdados inicializa(tdados *dados)
{ // iremos apontar para uma variabvel
    (*dados).size = -1;
    printf("Inicializa objeto em: %p\n", dados);
}

tdados e_inicializa(tdados dados)
{ // por não apontar para o endereço dá erro
    dados.size = 2;
    printf("E_Inicializa objeto em: %p\n", &dados);
    // o erro é que em vez de ir por endereço fdaz um fetch da variavel que causa problemas
    // o programa nã realiza o fetch
}

int main(int argc, char const *argv[])
{
    tdados dados;
    tdados edados;
    printf("Objeto dados em: %p\n", &dados);
    printf("Objeto edados em: %p\n", &edados);

    inicializa(&dados);
    e_inicializa(edados);

    fprintf(stdout, "Inicialização correta\nTamanho:\t%i\n", dados.size);
    fprintf(stdout, "Inicialização errada\nTamanho:\t%i\n", edados.size);
    return 0;
}

Output:

Objeto dados em: 0x7ffee5d479b0
Objeto edados em: 0x7ffee5d478a8
Inicializa objeto em: 0x7ffee5d479b0
E_Inicializa objeto em: 0x7ffee5d47560
Inicialização correta
Tamanho:        -1
Inicialização errada
Tamanho:        0

Note how the address in "initialize object" is the same as the "data object", that is, you are dealing with the object in that memory address.

 2
Author: sbrubes, 2019-03-19 11:59:22

You are passing to edados in the function, but since it is not by Pointer the value does not remain. The right thing would be to return in the function.

edados = e_inicializa(edados);

And put a return inside e_inicializa:

tdados e_inicializa(tdados dados) {
    dados.size = 2;
    return dados;
}

Also you have declared the functions as tdados and they do not return anything.

 -1
Author: Vinicius Morais, 2019-03-19 11:54:17