int *fillArray(int* array,const int arraySize, const int min, const int max)
{
srand(time(0));
for (size_t i = 0; i < arraySize; i++)
{
array[i] = rand % (max - min + 1) - min;
}
}
Asked
Active
Viewed 1,912 times
-3

D-RAJ
- 3,263
- 2
- 6
- 24

Ilyes Abdellatif
- 11
- 3
-
You likely meant `rand()`, with a pair of parens. The compiler thinks you are trying to apply the remainder operator to a function pointer, which of course doesn't make sense. – Igor Tandetnik Feb 16 '21 at 04:58
-
Unrelated, but see also [srand() — why call it only once?](https://stackoverflow.com/questions/7343833/srand-why-call-it-only-once). – dxiv Feb 16 '21 at 05:08
1 Answers
1
One error in your code is that rand
is a function. To get a random number using that function you need to call it like so:
int random = rand();
But in here: array[i] = rand % (max - min + 1) - min;
your calling the modulus operator on a function pointer. This is invalid. I guess what you were looking for is this:
array[i] = rand() % (max - min + 1) - min;
Another issue is that your not returning anything from the function. This will lead to undefined behavior. Make sure that your always return something (except for functions with a void
return type).
int *fillArray(int* array,const int arraySize, const int min, const int max)
{
srand(time(0));
for (size_t i = 0; i < arraySize; i++)
{
array[i] = rand() % (max - min + 1) - min;
}
return array; // Return the modified array.
}

D-RAJ
- 3,263
- 2
- 6
- 24