0

I'm trying to pass an int array as a pointer to a function. I'm currently getting the following error: expected 'int (*)[100]' but argument is of type 'int *' void.

void count_frequency(int *number) {
    int i;
    int len = sizeof number / sizeof(int);
    printf("%i\n", len);
    printf("reached here");
    for(i = 0; i < len; i++){
        printf("%i\n", &number[i]);
    }
}



int main(){
    int i;
    int table[MAX];
    int len = sizeof table / sizeof(int);
    printf("reached before loop\n");

    for(i = 0; i < len; i++){
        table[i] = random_in_range(0, 20); 
    }
    count_frequency(table);

    //printf("%i", sizeof(table) / sizeof(int));
    return 0;
}
Evan Trimboli
  • 29,900
  • 6
  • 45
  • 66
SpecialSnowflake
  • 945
  • 4
  • 16
  • 32
  • 1
    `int len = sizeof number / sizeof(int);` manually check what this returns on different sized arrays. – this Feb 12 '14 at 20:15
  • What returns random_in_range function? – Vincenzo Maggio Feb 12 '14 at 20:17
  • Read [Weird behavior when printing array in C?](http://stackoverflow.com/questions/18009725/weird-behavior-when-printing-array-in-c/18009736#18009736) – Grijesh Chauhan Feb 12 '14 at 20:18
  • possible duplicate of [C - SizeOf Pointers](http://stackoverflow.com/questions/18363037/c-sizeof-pointers) – Ed S. Feb 12 '14 at 20:20
  • random_in_range returns a random number between 0 - 20 – SpecialSnowflake Feb 12 '14 at 20:20
  • 1. Get rid of `int len = sizeof number / sizeof(int);`, which is very wrong. 2. The compilation error you're reporting is not within the code you provide; possibly in function `random_in_range`? or in the way you've defined `MAX`? – barak manos Feb 12 '14 at 20:20
  • @EdS. it's about a compile error... – mb84 Feb 12 '14 at 20:21
  • strange message. and `printf("%i\n", &number[i]);` : remove `&`. – BLUEPIXY Feb 12 '14 at 20:24
  • @mb84: True... though that error makes no sense given his code sample. – Ed S. Feb 12 '14 at 20:39
  • @mb84: But the error message states that it is *expecting* a pointer to an array, yet he is *passing* an `int*`. Those would be reversed if you were correct. The only type mismatch I see is in the `printf` call. – Ed S. Feb 12 '14 at 21:16
  • @EdS. oh, yes, my bad. Then maybe some double usage of `number` (typedef, macro, ...)?! – mb84 Feb 12 '14 at 21:24

2 Answers2

1

The code you posted is inconsistent with the error you're getting. The only wrong things are:

  • Remember that %d or %i require an integer passed as argument
  • I assume the "random_in_range" function uses the rand() as follows

     #include <stdio.h>
    
     #define MAX 100
    
    
     int random_in_range(int a, int b)//this function will generate a random number between specified range
     {
         return (a+rand()%(b-a+1));
     }
    
    
     void count_frequency(int *number) {
          int i;
          int len = sizeof number / sizeof(int);
          printf("%i\n", len);
          printf("reached here");
          for(i = 0; i < len; i++){
           printf("%d\n", number[i]);
           }
     }
    
    
    
     int main(){
          int i;
          int table[MAX];
          int len = sizeof table / sizeof(int);
          printf("reached before loop\n");
    
          for(i = 0; i < len; i++){
                table[i] = random_in_range(0, 20); 
          }
          count_frequency(table);
    
          printf("%i", sizeof(table) / sizeof(int));
          return 0;
     }
    

http://ideone.com/LG96iA

Marco A.
  • 43,032
  • 26
  • 132
  • 246
0

As far as I know, "sizeof number" is going to return the size of the pointer (4 bytes) rather than the size of the array.

CodeMonkey
  • 1,795
  • 3
  • 16
  • 46