Suppose I have the following code:
typedef struct
{
char **p;
} STRUCT;
int main()
{
STRUCT s;
*(s.p) = "hello";
printf("%s\n", *(s.p));
return 0;
}
which obviously doesn’t work, but it should show what I want to do. How would I go about initialising, accessing, printing, etc the array of strings in the structure?
You have two
*where you want just one, I think. Try:If you do really want to have the double indirection, you need to allocate some space for the pointer you’re dereferencing.
*(s.p)in your original program dereferences an uninitialized pointer. In this case:This second program allocates space for just one string pointer; if you want an array, just allocate the appropriate amount of space.