简体   繁体   中英

Segmentation Fault using pointer of structure

When I try to debug my program, I have error message like "Segmentation fault".

typedef struct
{
    int a;
    char *** tab;
}Operateur;

int main()
{
    char * chaine = "test";
    Operateur * emptyStruct = (struct Operateur *) malloc(sizeof(Operateur));

    emptyStruct->tab[0][0] = * chaine;
    return 0;
}

I would like to put the content of chaine in the first place of my array(tab).

Thanks.

The member tab is not initialized, you have to allocate it. For example:

Operateur * emptyStruct = malloc(sizeof(Operateur));
emptyStruct->tab = malloc(sizeof(char**) * 1);
emptyStruct->tab[0] = malloc(sizeof(char*) * 1);

Change the "1" to allocate a bigger array.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM