C言語についての質問です
Posted: 2015年10月22日(木) 16:38
各文字列の出現回数を数えるプログラムを作成しているのですが、セグメッションフォルトが出ました。
どこがおかしいのか教えてください。C言語始めたばかりです。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct cell{
struct cell *next;
char *word;
int count;
}CELL;
int main(int argc,char **argv){
count_word();
return(0);
}
struct cell *list = NULL;
void count_word(char *word){
CELL *p;
for(p=list;p!=NULL;p=p->next){
if(strcmp(word,p->word)==0){
p->count++;
return;
}
}
p=(struct cell*)malloc(sizeof(struct cell));
p->word=(char*)malloc(strlen(word)+1);
strcpy(p->word,word);
p->count=1;
p->next=list;
list=p;
printf("%s:%d\n",p->word,p->count);
}
どこがおかしいのか教えてください。C言語始めたばかりです。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct cell{
struct cell *next;
char *word;
int count;
}CELL;
int main(int argc,char **argv){
count_word();
return(0);
}
struct cell *list = NULL;
void count_word(char *word){
CELL *p;
for(p=list;p!=NULL;p=p->next){
if(strcmp(word,p->word)==0){
p->count++;
return;
}
}
p=(struct cell*)malloc(sizeof(struct cell));
p->word=(char*)malloc(strlen(word)+1);
strcpy(p->word,word);
p->count=1;
p->next=list;
list=p;
printf("%s:%d\n",p->word,p->count);
}