无法将(int *)转换为int

我只是试图从包含所有数字的文本文件中读取元素,并将其作为参数传递给下面给出的“frequency()”函数。但是,它显示一个错误,说“int类型的参数是与类型(int *)的参数不兼容“。 我尝试了将(int *)转换为int的所有内容,但结果却很糟糕。发布的是我的C代码。

void main() { FILE*file = fopen("num.txt","r"); int integers[100]; int i=0; int h[100]; int num; int theArray[100]; int n,k; int g; int x,l; while(fscanf(file,"%d",&num)>0) { integers[i]=num; k =(int)integers[i]; printf("%d\n",k); i++; } printf ("\n OK, Thanks! Now What Number Do You Want To Search For Frequency In Your Array? "); scanf("\n%d", &x);/*Stores Number To Search For Frequency*/ frequency(k,n,x); getch(); fclose(file); } void frequency (int theArray [ ], int n, int x) { int count = 0; int u; // printf("%d",n); for (u = 0; u < n; u++) { if ( theArray[u]==x) { count = count + 1 ; /*printf("\n%d",theArray[u]);*/ /* printf("\n%d",count);*/ } else { count = count ; } } printf ("\nThe frequency of %d in your array is %d ",x,count); } 

因此,想法是通过“num.txt”读取的元素存储在数组’k’中,并且必须在频率函数中传递相同的数组! 但是,在我的情况下,它说“类型int的参数与类型(int *)的参数不兼容。

  frequency(k, n, x); | | +---int ? 

 frequency (int theArray [ ], int n, int x) | | + accepts an int* 

将您的function称为

 frequency ( integers, i, x ); 

你永远不会在你的主体中初始化ntheArray ,只是声明它们不会在其他函数中神奇地传递它们。

当你调用frequency函数时,你将传入int k作为第一个参数。 我认为值得纠正你的陈述,即k是一个数组。 它不是。 您将其声明为int 。 这是你的类型错误的来源,因为它需要一个int * (因为int[]参数)。

也许你的意思是传入integers而不是k