-2
typedef struct stack
{
    struct stack *ptr;
    char* data; 
}*tStack;

typedef struct{
    tStack top;
}*tStack_ptr;

void Sinit(tStack_ptr s)
{
    s->top = NULL;
}

int main() {
    //stack
    tStack_ptr s;

    Sinit(s);

return 1;
}

When trying to assign top to NULL it gives me segmentation fault, any ideas? Does it have something to do with anonymous struct?

melpomene
  • 84,125
  • 8
  • 85
  • 148
Holajz
  • 47
  • 1
  • 4

1 Answers1

0
tStack_ptr s;  

variable 's' is just a pointer pointed to struct{tStack *top}, the system doesn't alloc any memory to a struct that it points to.

On a 32-bit PC, s takes 4 bytes in memory, same as other pointers, like 'int *'.

You can define tStack_ptr like this:

typedef struct{
    tStack top;
}tStack_ptr;

This just solve "segment fault", the sturct tStack, I am not sure what do you want.

wmlhust
  • 141
  • 1
  • 4