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;
}
11
  • 1
    int len = sizeof number / sizeof(int); manually check what this returns on different sized arrays. Commented Feb 12, 2014 at 20:15
  • What returns random_in_range function? Commented Feb 12, 2014 at 20:17
  • Read Weird behavior when printing array in C? Commented Feb 12, 2014 at 20:18
  • possible duplicate of C - SizeOf Pointers Commented Feb 12, 2014 at 20:20
  • random_in_range returns a random number between 0 - 20 Commented Feb 12, 2014 at 20:20

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.