#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;
}
文字配列を動的に割り当てようとしています。そこに文字列を入れ、ノードのデータ要素を文字配列にする。私がプログラムを実行すると、私はセグメンテーションフォルトを取得します。私はそれが上でラベル付けされた行からだと思う。私が理解していないのは、私は文章 - > arrを単語配列の最初の要素を指すようにしたことです。なぜそれはそれのようなクラッシュでしょうか?前もって感謝します。ポインタがcの文字の配列を指している
注:[必要](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-mallocはありません) 'malloc'の結果をキャストする – red0ct