How to access a pointer inside a pointer structure

2

I know that to access a normal variable inside a pointer structure I can use this syntax:

struct teste { 

    int valor;
    int *aponta;

};

struct teste testando, *testado;
testado = &testando;
testado -> valor = 10;

However, how to access the aponta pointer that is contained in the structure, using the testado ??

    
asked by anonymous 28.02.2018 / 04:07

1 answer

4

To make this pointer point to somewhere:

int teste2 = 123;
testado->aponta = &teste2;

To change the value of what is pointed to:

*(testado->aponta) = 456;

And to read the value of the variable:

*(testado->aponta)

Here's a test with the complete code:

#include <stdio.h>

struct teste { 
    int valor;
    int *aponta;
};

int main() {

    struct teste testando, *testado;

    testado = &testando;
    testado->valor = 10;
    int teste2 = 123;
    testado->aponta = &teste2;
    *(testado->aponta) = 456;
    printf("%d %d", teste2, *(testado->aponta));
}

The output is 456 456 . See here working on ideone.

    
28.02.2018 / 04:35