I have a struct with two pointers and an int variable. For some reason I am getting a segmentation fault at the line ptr->i=0;
. Why is that? I'm pointing to something in the memory, i
is not a pointer so it should be legal. Can anyone please explain what is going on with this? I did create memory for the struct and the two char pointers.
struct A_ {
char *a;
char *b;
int i;
};
typdef struct A_ StructA;
And then in my main()
I have the following:
StructA *ptr=malloc(sizeof(StructA));
ptr->a=malloc(sizeof(char));
ptr->b=malloc(sizeof(char));
ptr->i=0;