我正在尝试将int数组作为指向函数的指针传递。我目前收到以下错误: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;
}
答案 0 :(得分:1)
您发布的代码与您获得的错误不一致。唯一错误的是:
我假设“random_in_range”函数使用rand(),如下所示
#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;
}
答案 1 :(得分:0)
据我所知,“sizeof number”将返回指针的大小(4个字节),而不是数组的大小。