I have a function called int* abc()
which returns a pointer array and I called it int* num = abc();
Everything is fine until I want to loop through the array, which the sizeof(num)
returns 8 all the time.
my question is how am I supposed to loop through it?
Everything is fine until I want to loop through the array, which the sizeof(num) returns 8 all the time.
In your code, num
is a pointer. Its size never changes.
You cannot derive the number of elements from the pointer. You need to provide the number via another variable.
Either by adding a parameter like size_t *number_of_results
that is set to the correct number before the function returns.
Or by adding a sentinel value to the array.
E.g. if you do not allow negative values in your array, you could mark the last entry with -1
. Or 0
as it is done for strings in C.