我只是尝试从拥有所有数字的文本文件中读取元素,并将其作为参数传递给给定below.However的“read ()”函数,结果会出现一个错误,即“int类型的参数与类型(int*)的参数不兼容”。我尝试了一切将( int *)转换为int,但最后发布的miserably..below是我的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*)的参数不兼容。
发布于 2014-09-21 02:25:20
frequency(k, n, x);
|
|
+---int ?但
frequency (int theArray [ ], int n, int x)
|
|
+ accepts an int*调用您的函数作为
frequency ( integers, i, x );您从未在主函数中初始化n和theArray,简单地声明它们不会在其他函数中神奇地传递它们。
发布于 2014-09-21 02:20:51
当您调用frequency函数时,您将传入int k作为第一个参数。我认为值得纠正你关于k是数组的说法。事实并非如此。您将其声明为int。这是您的类型错误的来源,因为它需要一个int * (因为int[]参数)。
也许你是想通过integers而不是k
https://stackoverflow.com/questions/25955175
复制相似问题