前提・実現したいこと
複数行に渡る単語の入力において
各単語の現れる回数を知るために線形リストを用いて新しい単語の入力であればノードを追加し、リストにあればcount+1とするプログラムを作りたいのですが、コンパイルしたところ
線形リストのノード挿入時において, ある文字列をそのノードのメンバに代入したいのですがうまくいかず警告文も理解できないため。
ご教示願います。
発生している問題・エラーメッセージ
reduce.c: 関数 ‘insert’ 内: reduce.c:14:3: 警告: 2 番目の ‘strcpy’ の引数へ渡すときに整数からキャスト無しにポインタを作成しています [デフォルトで有効] strcpy(n->element, e); ^ In file included from reduce.c:2:0: /usr/include/string.h:125:14: 備考: expected ‘const char * __restrict__’ but argument is of type ‘elementtype’ extern char *strcpy (char *__restrict __dest, const char *__restrict __src) ^ reduce.c: 関数 ‘main’ 内: reduce.c:43:5: 警告: passing argument 2 番目の ‘insert’ の引数を渡すときにポインタからキャスト無しに整数を作成しています [デフォルトで有効] if(l->next == NULL) { insert(l, buf); } ^ reduce.c:11:6: 備考: expected ‘elementtype’ but argument is of type ‘char *’ void insert(list l, elementtype e) { ^
該当のソースコード
C
1#include<stdio.h> 2#include<string.h> 3#include<stdlib.h> 4 5char buf[128]; 6typedef char elementtype; 7 8struct node { int count; elementtype element[128]; struct node *next; }; 9typedef struct node* list; 10 11void insert(list l, elementtype e) { 12 list n; 13 n = (struct node *) malloc (sizeof (struct node)); 14 strcpy(n->element, e); 15 n->count = 0; 16 n->next = l->next; 17 l->next = n; 18} 19 20void print_list(list l) { 21 l = l->next; 22 while(l != NULL) { 23 printf("%s %d\n",l->element, l->count); 24 l = l->next; 25 } 26 printf("\n"); 27} 28 29int main(void) { 30 list l, head; 31 //ダミーノードを使います 32 l = (struct node *)malloc(sizeof(struct node)); 33 l->next = NULL; 34 head = l; 35 36 while(fgets(buf,sizeof(buf),stdin) !=NULL) { 37 while(l->next != NULL) { 38 //同じ文字列を持つリストがある場合count+1 39 if(strcmp(l->element, buf)==0) { l->count++; break;} 40 else { l = l->next; } 41 } 42 //もし同じ文字列を持つリストが存在しなかった場合 43 if(l->next == NULL) { insert(l, buf); } 44 l = head; 45 } 46 print_list(head); 47 return 0; 48}
###入出力例
入力
ねこ
ぶた
たぬき
ねこ
いぬ
ねこ
いぬ
出力
ねこ 3
ぶた 1
たぬき 1
いぬ 2
補足情報(FW/ツールのバージョンなど)
gccコンパイラ
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/10/31 09:19