gpt4 book ai didi

c - 尝试将 int* 传递给函数并在另一个函数内运行。int* 已在 main 中声明

转载 作者:行者123 更新时间:2023-11-30 16:32:32 26 4
gpt4 key购买 nike

主要内容[警告] 从不兼容的指针类型传递“calculatetextstatistics”的参数 1[注意] 预期为“int *”,但参数的类型为“int **”
计算文本统计

[警告] 传递“countwords”的参数 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("%d\n",countw);
fclose(p);
*n=countw;
return;
}

整个代码(经过一些更正)

注意我在某些 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;
}

最佳答案

这里有一个问题:

函数 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); 

关于c - 尝试将 int* 传递给函数并在另一个函数内运行。int* 已在 main 中声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50103375/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com