jokam
September 21, 2010, 1:48pm
1
How to determine array length when array of int-s is passed as parameter to the function?
void someFunction(int arr[])
{
int lengthOfArray = ... ?
}
Solution from site:
sizeof(myInts)/sizeof(int)
does not work. It returns 1 always when variable is passed as parameter.
It isn't possible to simply do what you want in C.
There's usually a workaround, depending on how you use the array, like using the macro you found as an argument to the function.
void someFunction(int arr[], int length)
{
}
someFunction (myArray, sizeof (myArray) / sizeof (myArray[0]));
jokam
September 21, 2010, 2:28pm
3
Yeah, thanks!
It makes sense to me now. In my case, it was the length of pointer to array I think and it's always one. Correct me if I'm wrong
jokam
September 21, 2010, 3:57pm
5
My bad again!
I've used it in a random(min, max) function as max, and it returns random number between min and max-1.
Oh I really need to learn C again..