简体   繁体   English

无法将(int *)转换为int

[英]Unable to convert (int*) to int

I'm just trying to read the elements from a text file that has all the numbers and pass it as an argument to the "frequency()" function given below.However,It shows up an error saying that "argument of type int is incompatible with argument of type (int*)" . 我只是试图从具有所有数字的文本文件中读取元素,并将其作为参数传递给下面给出的“ frequency()”函数。但是,它显示了一个错误,指出“ int类型的参数是与(int *)类型的参数不兼容”。 I tried everything to convert (int*) to int but ended up miserably..below posted is my C code. 我尝试了一切将(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);


  }

So the idea is that the elements read through "num.txt" are stored in an array 'k' and the same array has to be passed in the frequency function! 因此,想法是将通过“ num.txt”读取的元素存储在数组“ k”中,并且必须在频率函数中传递相同的数组! However,in my case it is saying "argument of type int is incompatible with argument of type(int*). 但是,在我的情况下,这是说“ int类型的参数与type(int *)的参数不兼容。

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

But

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

Call your function as 调用您的函数为

frequency ( integers, i, x );

You never initialized n and theArray in your main, simply declaring them will not magically pass them in other functions. 您从未在主程序中初始化ntheArray ,只是声明它们不会神奇地将它们传递给其他函数。

When you call the frequency function, you're passing in the int k as the first argument. 调用frequency函数时,您将传入int k作为第一个参数。 I think it's worth correcting your statement that k is an array. 我认为值得纠正您关于k是数组的陈述。 It is not. 它不是。 You declare it as an int . 您将其声明为int This is where your type error is coming from because it expects an int * (because of the int[] argument). 这是类型错误的来源,因为它需要一个int * (因为int[]参数)。

Perhaps you mean to pass in integers instead of k ? 也许您的意思是传递integers而不是k

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM