0

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.

Fuzion
  • 11
  • 1
  • 1
    You have not allocated any memory for the 3-star pointer `tab`, well 4-star actually with the struct pointer. – Weather Vane Oct 29 '16 at 15:03
  • 1
    another three-star programmer in the works :) – Jean-François Fabre Oct 29 '16 at 15:04
  • 1
    `struct Operateur` is not a thing. And [you shouldn't cast the result of `malloc`](https://stackoverflow.com/q/605845). – Siguza Oct 29 '16 at 15:04
  • tab is just a pointer. You have not allocated memory to anything it points to. If you are just starting to lean C, maybe read up more about pointers, and start with simpler examples. – OldProgrammer Oct 29 '16 at 15:04
  • Please enable compiler warnings, and deal with them *before* running the program - ***especially pointer warnings***. "warning C4047: '=': 'char *' differs in levels of indirection from 'char'" and "warning C4133: 'initializing': incompatible types - from 'Operateur*' to 'Operateur *'" – Weather Vane Oct 29 '16 at 15:08

1 Answers1

0

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.

Hugal31
  • 1,610
  • 1
  • 14
  • 27