質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
C

C言語は、1972年にAT&Tベル研究所の、デニス・リッチーが主体となって作成したプログラミング言語です。 B言語の後継言語として開発されたことからC言語と命名。そのため、表記法などはB言語やALGOLに近いとされています。 Cの拡張版であるC++言語とともに、現在世界中でもっとも普及されているプログラミング言語です。

コンパイルエラー

コンパイルのフェーズで生成されるエラーです。よく無効なシンタックスやタイプが含まれているとき発生します。

Q&A

解決済

2回答

6417閲覧

構造体のリスト構造の課題なんですがコンパイルエラーの理由がわかりません

yukikona

総合スコア12

C

C言語は、1972年にAT&Tベル研究所の、デニス・リッチーが主体となって作成したプログラミング言語です。 B言語の後継言語として開発されたことからC言語と命名。そのため、表記法などはB言語やALGOLに近いとされています。 Cの拡張版であるC++言語とともに、現在世界中でもっとも普及されているプログラミング言語です。

コンパイルエラー

コンパイルのフェーズで生成されるエラーです。よく無効なシンタックスやタイプが含まれているとき発生します。

0グッド

0クリップ

投稿2019/12/24 11:50

前提・実現したいこと

構造体について勉強したばかりでして、多くの自分では理解不可能なコンパイルエラーが出てきてしまいます。
それぞれの対処法をどうか教えてください。

発生している問題・エラーメッセージ

report4-pro.c:6:29: warning: declaration of 'struct record' will not be visible
outside of this function [-Wvisibility]
void add(char *trans,struct record *topad);
^
report4-pro.c:7:25: warning: declaration of 'struct record' will not be visible
outside of this function [-Wvisibility]
void displaylist(struct record *topad);
^
report4-pro.c:16:6: error: conflicting types for 'add'
void add(char *trans,struct record *topad){
^
report4-pro.c:6:6: note: previous declaration is here
void add(char *trans,struct record *topad);
^
report4-pro.c:22:18: warning: comparison of array 'list->word' equal to a null
pointer is always false [-Wtautological-pointer-compare]
if(list->word=='\0'){
report4-pro.c:51:6: error: conflicting types for 'displaylist'
void displaylist(struct record *topad){
^
report4-pro.c:7:6: note: previous declaration is here
void displaylist(struct record *topad);
^
report4-pro.c:93:23: warning: incompatible pointer types passing
'struct record *' to parameter of type 'struct record *'
[-Wincompatible-pointer-types]
add(trans,topad);

report4-pro.c:6:37: note: passing argument to parameter 'topad' here
void add(char *trans,struct record *topad);
^
report4-pro.c:101:17: warning: incompatible pointer types passing
'struct record *' to parameter of type 'struct record *'
[-Wincompatible-pointer-types]
displaylist(topad);

report4-pro.c:7:33: note: passing argument to parameter 'topad' here
void displaylist(struct record *topad);

該当のソースコード

c

1#include<stdio.h> 2#include<stdlib.h> 3#include<ctype.h> 4#include<string.h> 5 6void add(char *trans,struct record *topad); 7void displaylist(struct record *topad); 8 9struct record{ 10 char word[100]; 11 struct record *next; 12 struct record *back; 13 int frequency; 14}; 15 16void add(char *trans,struct record *topad){ 17 struct record *list; 18 struct record *newstr; 19 list = topad; 20 list->frequency=1; 21 while(0==0){ 22 if(list->word=='\0'){ 23 strcpy(list->word,trans); 24 break; 25 } 26 if(strcasecmp(list->word,trans)<0) list = list->next; 27 if(strcasecmp(list->word,trans)==0){ 28 list->frequency++; 29 break; 30 } 31 if(strcasecmp(list->word,trans)>0){ 32 newstr = (struct record*)malloc(sizeof(struct record)); 33 strcmp(newstr->word,trans); 34 list->back->next=newstr; 35 newstr->back=list->back; 36 list->back=newstr; 37 newstr->next=list; 38 break; 39 } 40 if(list->next==NULL){ 41 newstr = (struct record*)malloc(sizeof(struct record)); 42 strcmp(newstr->word,trans); 43 list->next=newstr; 44 newstr->back=list; 45 newstr->next=NULL; 46 break; 47 } 48 } 49} 50 51void displaylist(struct record *topad){ 52 struct record *list; 53 list=topad; 54 while(list!=NULL){ 55 printf("word:%s frequency:%d",list->word,list->frequency); 56 list=list->next; 57 } 58} 59 60int main(int argc,char *argv[]){ 61 struct record Topad; 62 struct record *topad; 63 topad=&Topad; 64 char buf[5120]; 65 char key[100]; 66 char trans[100]; 67 int wordnum=0; 68 int keynum=0; 69 int firsti,letnum,t,p,i; 70 71 printf("キーワードを入力してください\n"); 72 scanf("%s",key); 73 FILE *fp; 74 fp=fopen(argv[1],"r"); 75 while(fgets(buf,sizeof(buf),fp) != NULL){ 76 printf("%s",buf); 77 i=0; 78 p=0; 79 while(*(buf+i)!='\0'){ 80 while(isalnum(*(buf+i))==0&&*(buf+i)!='\0'){ 81 i++; 82 } 83 firsti=i; 84 if(*(buf+i) == '\0') break; 85 while(isalnum(*(buf+i))!=0){ 86 i++; 87 } 88 wordnum++; 89 letnum = i-firsti; 90 for(t=0;t<letnum;t++){ 91 *(trans+t)=*(buf+firsti+t); 92 } 93 add(trans,topad); 94 if(strcasecmp(key,trans)==0) keynum++; 95 memset(trans, '\0', sizeof(trans)); 96 while(isalnum(*(buf+i))==0&&*(buf+i)!='\0'){ 97 i++; 98 } 99 } 100 } 101 displaylist(topad); 102 printf("\n単語数=%d\nキーワード数=%d\n出現頻度=%f\n",wordnum,keynum,(float)keynum/(float)wordnum); 103 fclose(fp); 104 exit(0); 105}

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

y_waiwai

2019/12/24 11:56

このままではコードが読みづらいので、質問を編集し、<code>ボタンを押し、出てくる’’’の枠の中にコードを貼り付けてください
guest

回答2

0

警告の、

declaration of 'struct record' will not be visible outside of this function

を訳すと、「'struct record'の宣言はこの関数の外では見えません(=有効ではありません)」
という意味です。

次のエラーの、関数addのプロトタイプ宣言が一致しないというのも、これから来ていると思われます。
他の、型不一致(type conflict)関係の警告も同一原因でしょう。

次の警告の、

comparison of array 'list->word' equal to a null pointer is always false

を訳すと、'list->word'は配列なので、NULLと=で比較すると常に偽(=常に等しくない)
という意味です。

エラーメッセージは、難しい文法を使ってないので、辞書を引きながらなら、読めると思います。
Google翻訳を使ってもいいし。

投稿2019/12/24 12:02

otn

総合スコア84423

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

ベストアンサー

void add(char *trans,struct record *topad);

struct record が定義されていない、というエラー。
こいつの定義をこの関数定義の前に持ってこよう

if(list->word=='\0'){

list->word はchar* だけど、'\0'と比較している。型があってない、というエラー

投稿2019/12/24 11:58

編集2019/12/24 12:03
y_waiwai

総合スコア87719

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

yukikona

2019/12/24 12:18

ありがとうございます! 一気に解決しました!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問