It's cool i can use a while |
No, you can't. The code Jackson posted won't work in any useful way, and will either loop infinitely causing your program to hang, or else will crash.
Something similar might work
if you've done something special to your array, like put in a special number that's supposed to mark the end of the array, but even then you'd have to de-reference the pointer and check the value of the object it's pointing too, rather than the pointer itself, e.g.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
const int END_ARRAY_MARKER = 12345;
void myFunc()
{
int arr[5] = {1, 2, 3, 4, END_ARRAY_MARKER};
getAverage(arr);
}
void getAverage(int *arr)
{
// Use a second pointer, so that arr still points to the start of the array in case you need it elsewhere
int *arrPtr = arr;
while (*arrPtr != END_ARRAY_MARKER)
{
// Do something...
arrPtr ++;
}
}
|
But why bother? You'll have to remember to add that marker at the end of every array, and if you ever legitimately want to use that number in an array, then it will incorrectly think it's reached the end of the array too soon.
You're better off using a vector, which will manage this stuff for you, unless efficiency is really critical, in which case pass the size in along with the array. It's not
that onerous to pass an extra int.
Oh, and beware of advice from Jackson Marie. He/she is, at best, an enthusiastic beginner, and often gives advice that is bad.