0

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*).

  • You are passing `int` to `int[]`. I think you should pass `integers` to it. – Ankush Sep 21 '14 at 02:23
  • 1
    Don't use `void main()` in general — see [What should `main()` return in C and C++](http://stackoverflow.com/questions/204476/what-should-main-return-in-c-and-c/18721336#18721336) for more information. – Jonathan Leffler Sep 21 '14 at 02:38
  • This cast `k =(int)integers[i];` is worrying. It isn't necessary to convert an `int` to an `int` with a cast — so why did you add the cast and what don't you understand that made you think the cast was a good idea? – Jonathan Leffler Sep 21 '14 at 02:39

2 Answers2

2
 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.

P0W
  • 46,614
  • 9
  • 72
  • 119
0

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?

cjubb39
  • 454
  • 1
  • 5
  • 15
  • this is fine i'm trying to print the elements that integers stored ..However i'm getting something like 1375076.So,if i want to get the elements stored in the array integers what am i supposed to do? – rishanth chavali Sep 21 '14 at 03:54