【问题标题】:Trying to pass int* to function and to function inside another function.int* was declared in main试图将 int* 传递给函数并在另一个 function.int* 中函数在 main 中声明
【发布时间】:2023-03-18 12:35:01
【问题描述】:

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

[警告] 传递 'countwords' 的参数 2 使指针从整数而不进行强制转换 [注意] 期望 'int *' 但参数是 'int' 类型

如何将我计算的单词数(n)从 countwords 传递给其他函数而不发出任何警告?

程序按原样运行(更新:但是当我将其粘贴到主(非项目)错误弹出窗口时)

#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 中的更正代码,它也不起作用(运行但没有做它应该做的事情,意思是它不计算文件中正确的单词数,其他所有内容都是错误的。)真正的问题是程序如何在项目上运行,但是当我将它粘贴到 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;
}

【问题讨论】:

  • n 已经是一个指针,您不应该使用 addresof (&amp;) 运算符将其传递给您的函数。

标签: c


【解决方案1】:

这里有一个问题:

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

【讨论】:

  • 现在我得到 [错误] 从 'int' 到 'int*' 的无效转换 [-fpermissive]
  • and 7 [Note] 初始化'void countwords(FILE*, int*)'的参数2
  • 这个countuniquewords(p,*n);createhistogram(p,*n); 怎么样?他们也应该改变
【解决方案2】:

一次只关注一个函数,因此一次注释掉剩余的函数 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);
}

此外,当您提出关于 SO 的问题时,您不想只说“它没有做它应该做的事情”,因为这很明显,而且根本无法帮助我们帮助您。您应该提供文本文件“mytext.txt”的示例。为此,我刚刚制作了一个包含“一二三四”的文本文件,它会打印出来

字数:4

字符数:18

不带空格的字符数:15

这可能无关紧要,因为这似乎是家庭作业,但您不需要继续打开和关闭文件。

打开一次,然后在每个函数调用rewind(p);fseek(p, 0, SEEK_SET); 之后调用下一个函数。完成后在最后关闭文件。我更新了我的答案以反映这一点。

【讨论】:

  • 我做了一个两行的文本,它打印出正确的结果。
  • @Tyler 这取决于您是否尝试将换行符 \n 算作一个字符
  • 该文本文件的每一行末尾都有一个换行符。因此,如果您不知道这一点,那么也许这就是您认为得到错误结果的原因。
  • 会试一试。只需要用我的直方图计算一下
猜你喜欢
  • 1970-01-01
  • 2017-04-12
  • 2011-10-12
  • 2015-05-09
  • 2021-02-22
  • 1970-01-01
  • 2021-08-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多