Problems with dynamic and struct allocation

1

I want to create a struct where there will be a vector of structs ... The way I thought about doing this was something like this:

typedef struct{
    char *teste_str;
    int teste_int;
}TESTE_A;

typedef struct{
    TESTE_A **t;
}TESTE_B;

TESTE_B teste;

int main(void)
{
    teste.t = (TESTE_A**)malloc(3 * sizeof(TESTE_A*));
    teste.t[0]->teste_int = 25;
    printf("%d\n", teste.t[0]->teste_int);
    return 0;
}

But why does not the member value change and the program give error?

    
asked by anonymous 21.05.2017 / 15:33

1 answer

1

I considered you want to do C. The biggest problem is the% po_de% pointer pointer that does not make sense. In fact the names are pretty bad, the code is incomplete and does not seem to make any sense.

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

typedef struct {
    char *teste_str;
    int teste_int;
} TesteA;

typedef struct {
    TesteA *t;
} TesteB;

int main(void) {
    TesteB teste;
    teste.t = malloc(3 * sizeof(TesteA));
    teste.t[0].teste_int = 25;
    printf("%d\n", teste.t[0].teste_int);
}

See Running on Repl.it . Also put it on GitHub for future reference .

    
21.05.2017 / 15:50