C 试图将int*传递给函数和另一个函数内的函数。int*在main中声明
大体上 [警告]正在从不兼容的指针类型传递“calculatetextstatistics”的参数1 [注意]应为“int*”,但参数的类型为“int**”C 试图将int*传递给函数和另一个函数内的函数。int*在main中声明,c,C,大体上 [警告]正在从不兼容的指针类型传递“calculatetextstatistics”的参数1 [注意]应为“int*”,但参数的类型为“int**” 在CalculateExtStatistics中 [警告]传递'countwords'的参数2会使指针从整数变为不带强制转换的整数 [注意]应为“int*”,但参数的类型为“int” 如何将从countwords计算的字数(n)传递到其他函数而不发出任何警告 程序按原样工作(更新:但当我将其粘贴到主(非项目)上时,会弹出错误) #包括 #包
在CalculateExtStatistics中 [警告]传递'countwords'的参数2会使指针从整数变为不带强制转换的整数 [注意]应为“int*”,但参数的类型为“int” 如何将从countwords计算的字数(n)传递到其他函数而不发出任何警告 程序按原样工作(更新:但当我将其粘贴到主(非项目)上时,会弹出错误)
#包括
#包括
#包括
int getchoice(void);
void inserttextfromfile();
作废printtextdata(文件*);
void countwords(FILE*,int*);
void calculatextstatistics(int*);
无效计数字符(文件*);
void countcharsbutspaces(文件*);
void countuniquewords(FILE*,int*);
void createhistogram(文件*,int*);
int cntocc(int[],int[],int,int);
int main(){
int a,g;
int*n=&g;
而((a=getchoice()){
开关(a){
案例1:
打破
案例2:;
打破
案例3:;
打破
案例4:;
打破
案例5:CalculateExtStatistics(&n);
打破
案例6:;
打破
默认:中断;
}
}
返回0;
}
int getchoice(){
INTA;
scanf(“%d”和“&a”);
返回a;
}
void calculatextstatistics(int*n){
文件*p;
p=fopen(“mytext.txt”、“rt”);
可数字(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”);
直方图(p,*n);
}
void countwords(文件*p,int*n){
int countw=0;
字符字持有者[10]=“”;
字符字表[60][10];
对于(;(fscanf(p,“%s”,单词持有者))!=EOF;countw++);
printf(“%d\n”,countw);
fclose(p);
*n=countw;
返回;
}
整个代码(经过一些更正)
注意,在第一个和最后两个函数中,我有一些FOR循环*n(如果这是问题的话…)
在我的项目中,参数的状态与上面一样,警告和注释会弹出,但没有错误,程序会执行应该执行的操作。但是如果我将代码复制并粘贴到一个新的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("%s\n",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("%d\n",countw);
fclose(p);
*n=countw;
return;
}
#包括
#包括
#包括
int getchoice(void);
void inserttextfromfile();
作废printtextdata(文件*);
void countwords(FILE*,int*);
void calculatextstatistics(int*);
无效计数字符(文件*);
void countcharsbutspaces(文件*);
void countuniquewords(FILE*,int*);
void createhistogram(文件*,int*);
int cntocc(int[],int[],int,int);
int main(){
int a,g;
int*n=&g;
而((a=getchoice()){
开关(a){
案例1:
打破
案例2:;
打破
案例3:;
打破
案例4:;
打破
案例5:CalculateExtStatistics(n);
打破
案例6:;
打破
默认:中断;
}
}
返回0;
}
int getchoice(){
INTA;
scanf(“%d”和“&a”);
返回a;
}
//void inserttextfromfile(){
//文件*p;
//p=fopen(“mytext.txt”、“rt”);
//打印文本数据(p);
//fclose(p);
//返回;
//}
//无效printtextdata(文件*p){
//int g;
//字符空间[10]=“”;
//对于(;(g=fscanf(p,“%s”,空格))!=EOF;printf(“%s\n”,空格));
//返回;
//}
void calculatextstatistics(int*n){
文件*p;
p=fopen(“mytext.txt”、“rt”);
可数字(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”);
绘制直方图(p,n);
}
void countwords(文件*p,int*n){
int countw=0;
字符字持有者[10]=“”;
字符字表[60][10];
对于(;(fscanf(p,“%s”,单词持有者))!=EOF;countw++);
printf(“%d\n”,countw);
fclose(p);
*n=countw;
返回;
}
这里有一个问题:
函数CalculateExtStatistics
需要一个int*
,但在main
中用int**
调用它
这里
那就这么做吧
case 5:calculatetextstatistics(n) ;
countwords(p,n);
下一个问题非常类似:
countwords(p,*n); // Since n is int* then *n is an int
但是countwords
需要int*
那就这么做吧
case 5:calculatetextstatistics(n) ;
countwords(p,n);
这似乎同样适用于
countuniquewords(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: %d\n",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: %d\n",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: %d\n",countcbs);
}
另外,当你问一个问题,所以你不想只是说“它没有做它应该做的事”,因为这是很明显的,根本无助于我们帮助你。您应该提供文本文件“mytext.txt”的示例。为此,我制作了一个包含“一二三四”的文本文件,并打印出来
字数:4
字符数:18
不带空格的字符数:15
这可能无关紧要,因为这看起来像是家庭作业,但您不需要一直打开和关闭文件
打开一次,然后在每次函数调用后倒带(p)代码>或fseek(p,0,SEEK\u集)代码>然后调用下一个函数。然后在结束时关闭该文件。我更新了我的答案以反映这一点。nvoid 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: %d\n",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: %d\n",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: %d\n",countcbs);
}