问题
I have two structure in c
struct data{
char *name;
};
struct lst{
struct lst *next;
struct table *data;
};
when I'm trying to assign a name like
l->data->name = d->name; printf("%s",l->data->name);
it gives segmentation fault. So is it because read-only memory or caused by another reason ?
ok I solved the problem : ) I've done :
l->data = d; d has the name already :) thanks all
回答1:
Just before you do that segmentation-violation-causing instruction, insert:
printf( "%p\n", l);
printf( "%p\n", l->data);
printf( "%p\n", d);
printf( "%p\n", d->name);
and see which one is set to NULL (or an invalid value).
Your segmentation violation is almost certainly caused by an uninitialized pointer.
回答2:
I can be caused by a member pointing into an invalid zone.
回答3:
l->data is most likely NULL
来源:https://stackoverflow.com/questions/1935642/segmentation-fault-while-assigning-structure-members-in-c