IN MAIN [警告] 从不兼容的指针类型传递"计算文本统计信息"的参数 1 [注意] 预期的"int *",但参数在计算文本统计中属于"int **">
类型
[警告] 传递"计数词"的参数 2 使指针从整数不带强制转换 [注意] 预期的"int *",但参数的类型为"int">
我该怎么做才能将我计算的单词数 (n( 从计数词传递到其他函数而没有任何警告?
程序按原样工作(更新:但是当我将其粘贴到主(而不是项目(上时,会弹出错误(
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int getchoice(void);
void inserttextfromfile();
void printtextdata(FILE *);
void countwords(FILE *,int *);
void calculatetextstatistics(int *);
void countchars(FILE *);
void countcharsbutspaces(FILE *);
void countuniquewords(FILE *,int* );
void createhistogram(FILE *,int* );
int cntocc(int [],int [],int,int);
int main(){
int a,g;
int *n=&g;
while((a=getchoice())){
switch (a){
case 1:
break;
case 2: ;
break;
case 3: ;
break;
case 4: ;
break;
case 5:calculatetextstatistics(&n) ;
break;
case 6: ;
break;
default: break;
}
}
return 0;
}
int getchoice(){
int a;
scanf("%d",&a);
return a;
}
void calculatetextstatistics(int *n){
FILE *p;
p=fopen("mytext.txt","rt");
countwords(p,*n);
p=fopen("mytext.txt","rt");
countchars(p);
p=fopen("mytext.txt","rt");
countcharsbutspaces(p);
p=fopen("mytext.txt","rt");
countuniquewords(p,*n);
p=fopen("mytext.txt","rt");
createhistogram(p,*n);
}
void countwords(FILE *p,int *n){
int countw=0;
char wordholder[10]=" ";
char wordlist[60][10];
for (;(fscanf(p,"%s",wordholder))!= EOF;countw++);
printf("%dn",countw);
fclose(p);
*n=countw;
return;
}
整个代码(经过一些更正(
请注意,在第一个和最后 2 个函数中,我在某些 FOR 循环中具有 *n(只是如果这是概率......
在我的项目中,参数的状态如上所示,弹出警告和注释,但没有错误,程序执行应该做的事情。但是,如果我将代码复制并粘贴到新的 main 上,它会显示错误(不运行(,下面的代码是来自 main 的更正代码,它也不起作用(确实运行但没有做它应该做的事情,这意味着它没有计算文件中正确的字数,其他一切都是错误的。真正的问题是程序如何在项目上运行,但是当我将其粘贴到main上时,它不会
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int getchoice(void);
void inserttextfromfile();
void printtextdata(FILE *);
void countwords(FILE *,int *);
void calculatetextstatistics(int *);
void countchars(FILE *);
void countcharsbutspaces(FILE *);
void countuniquewords(FILE *,int* );
void createhistogram(FILE *,int* );
int cntocc(int [],int [],int,int);
int main(){
int a,g;
int *n=&g;
while((a=getchoice())){
switch (a){
case 1:
break;
case 2: ;
break;
case 3: ;
break;
case 4: ;
break;
case 5:calculatetextstatistics(n) ;
break;
case 6: ;
break;
default: break;
}
}
return 0;
}
int getchoice(){
int a;
scanf("%d",&a);
return a;
}
//void inserttextfromfile(){
// FILE *p;
// p=fopen("mytext.txt","rt");
// printtextdata(p);
// fclose(p);
// return;
//}
//void printtextdata(FILE *p){
// int g;
// char space[10]=" ";
// for (;(g=fscanf(p,"%s",space))!= EOF;printf("%sn",space));
// return;
//}
void calculatetextstatistics(int *n){
FILE *p;
p=fopen("mytext.txt","rt");
countwords(p,n);
p=fopen("mytext.txt","rt");
countchars(p);
p=fopen("mytext.txt","rt");
countcharsbutspaces(p);
p=fopen("mytext.txt","rt");
countuniquewords(p,n);
p=fopen("mytext.txt","rt");
createhistogram(p,n);
}
void countwords(FILE *p,int *n){
int countw=0;
char wordholder[10]=" ";
char wordlist[60][10];
for (;(fscanf(p,"%s",wordholder))!= EOF;countw++);
printf("%dn",countw);
fclose(p);
*n=countw;
return;
}
这里有一个问题:
该函数calculatetextstatistics
需要一个int*
,但您在 main
中使用int**
调用它。
这里
int *n=&g; // n is a int*
....
case 5:calculatetextstatistics(&n) ; // Here you take address-of and making it int**
所以只要做
case 5:calculatetextstatistics(n) ;
下一个问题非常相似:
countwords(p,*n); // Since n is int* then *n is an int
但countwords
预计会出现int*
所以只要做
countwords(p,n);
这似乎同样适用于
countuniquewords(p,*n);
和
createhistogram(p,*n);
一次只关注一个函数,因此一次注释掉 1 个剩余函数,以确保您了解错误所在。我也不确定您是否出于某种原因不允许使用while
循环,但是您读取文件的方式对我来说有点奇怪,所以我在此答案中更改了它。
void calculatetextstatistics(int *n){
FILE *p;
p=fopen("mytext.txt","rt");
countwords(p,n);
rewind(p);
countchars(p);
rewind(p);
countcharsbutspaces(p);
rewind(p);
#if 0
countuniquewords(p,n);
rewind(p);
createhistogram(p,n);
#endif
fclose(p);
}
void countwords(FILE *p,int *n){
int countw=0;
char wordholder[10]=" ";
char wordlist[60][10];
// for (;(fscanf(p,"%s",wordholder))!= EOF;countw++);
while (fscanf(p,"%s",wordholder) != EOF) {
countw++;
}
printf("Number of words: %dn",countw);
*n=countw;
return;
}
void countchars(FILE *p){
char ch;
int countc=0;
while (fscanf(p,"%c",&ch)!=EOF) {
if (ch != 'n')
countc++;
}
printf("Number of characters: %dn",countc);
}
void countcharsbutspaces(FILE *p){
char ch;
int countcbs=0;
// for(;(fscanf(p,"%c",&ch)!=EOF);countcbs++,countcbs-=(ch==' '));
while (fscanf(p,"%c",&ch)!=EOF) {
if (ch != 'n' && ch != ' ')
countcbs++;
}
printf("Number of Characters w/o Spaces: %dn",countcbs);
}
此外,当您在SO上提出问题时,您不想只是说"它没有做它应该做的事情",因为这很明显,根本无法帮助我们帮助您。您应该提供文本文件"mytext.txt"的示例。为此,我刚刚制作了一个包含"一二三四"的文本文件,然后打印
字数:4
字符数:18
不带空格的字符数:15
这可能无关紧要,因为它看起来像家庭作业,但您不需要继续打开和关闭文件。
打开它一次,然后在每次函数调用rewind(p);
或fseek(p, 0, SEEK_SET);
之后,然后调用下一个函数。然后在完成文件后关闭文件。我更新了我的答案以反映这一点。