前提・実現したいこと
構造体について勉強したばかりでして、多くの自分では理解不可能なコンパイルエラーが出てきてしまいます。
それぞれの対処法をどうか教えてください。
発生している問題・エラーメッセージ
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}
回答2件
あなたの回答
tips
プレビュー