Hi. This code:
results in this output:Code:struct node { int data[10]; }; int main() { struct node* head; int x; head->data[0] = 42; x = head->data[0]; printf(" x is %d\n",x); }
" x is 42
Segmentation fault"
Explanation and solution appreciated
Hi. This code:
results in this output:Code:struct node { int data[10]; }; int main() { struct node* head; int x; head->data[0] = 42; x = head->data[0]; printf(" x is %d\n",x); }
" x is 42
Segmentation fault"
Explanation and solution appreciated
You never allocate memory for head. You need to make it an actual struct node (no * pointer business) and use the . operator instead of ->, or you need to malloc space for a struct node and have head point to the malloc'ed memory.
. is for a struct (per my first suggestion, dropping the * and using a struct), -> is for a pointer to a struct.
Good question - you tell us after you look it up ;-)
It has to do with whether you are dealing with an actual structure (typically one that is locally declared) or a pointer to a structure (as it would typically be if you passed it in as a parameter from a calling function)