rishanth chavali
rishanth chavali

Reputation: 115

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*)" . I tried everything to convert (int*) to int but ended up miserably..below posted is my C code.

  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! However,in my case it is saying "argument of type int is incompatible with argument of type(int*).

Upvotes: 0

Views: 142

Answers (2)

P0W
P0W

Reputation: 47794

 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.

Upvotes: 2

cjubb39
cjubb39

Reputation: 464

When you call the frequency function, you're passing in the int k as the first argument. I think it's worth correcting your statement that k is an array. It is not. You declare it as an int. This is where your type error is coming from because it expects an int * (because of the int[] argument).

Perhaps you mean to pass in integers instead of k?

Upvotes: 0

Related Questions