#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct node
{
char *arr;
struct node *next;
} node;
int main()
{
char *word = (char *)malloc(sizeof(char) * 20);
strcpy(word, "jon jones");
node *sentence;
sentence->arr = word; //Problem here
printf("%s", sentence->arr);
return 0;
}
I am trying to dynamically allocate a character array. Put a string in there and then make the data element of the node point to the character array. When I run the program I get a segmentation fault. I suspect it is from the line that I labeled above. What I do not understand is, I made sentence->arr point to the first element of the word array. Why would it crash like that? Thanks in advance.