[SOLVED]Getting array lenght inside a function

Hello.

I need a function that would use array length, but I'm only able to get around passing the value to the function as a parameter.

Here's a test code:

int array1[] = {25,24,26,23,28,24,26,23,27,23,26,22,24,26,24,23,25,26,4,24,25,34,35};

void setup(){
  Serial.begin(57600);
  int getlength=function(array1);
  Serial.println(getlength);
}

void loop(){}

int function(int array[])
{
  int leng=sizeof(array)/sizeof(array[0]);
  return leng;
}

This will return the function length incorrectly as 1.

But I'm able the pass the value to the function in the parameters:

int getlength=function(array1,sizeof(array1)/sizeof(array1[0]));

.......

int function(int array[],int leng)
{
  return leng;
}

Which will return the correct value of 23.

But this seems a waste of space to call that "sizeof(array1)/sizeof(array1[0])" parameter using the specific array name every time I need that function. I don't see why I'm not able to pass the array name and get the size inside the function. What am I missing?

Nothing. That is the way it has always been with C. You can just calculate the size as a global variable with const if you want to save space. Since in C, the array size must be known beforehand, the array size calculation is done at compile time, not run time.

What you can do is define a class that wraps the C array that does this administration, even dynamic. Check the String class to see how things can be done (although I advice not to use these dynamic classes but that is another discussion)

guix:
That still won't work inside a function.

Sorry I misunderstood. I thought he wanted a function that returned the # of elements in an array.

KeithRB:
Since in C, the array size must be known beforehand, the array size calculation is done at compile time, not run time.

I read so too, but I was surprised that the sizeof calculation worked in function parameter while program running, I figured it should work inside the function too.
So I should store the array size in variable when compiling? Will the sizeof function get corrupted sometime as the "array decays into pointer"? That would cost an extra stored integer for each array, just for the length.

Welcome to scope. Note, that inside the function, array is a pointer to the array, not the array itself. That is why you got one. In C, the only way to get the size of the array inside the function is to pass it as a parameter.

The sizeof is determined at compile time, so there is no danger of "corrupting" it. Whether you use a const variable, the calculation or a #define makes no difference to the compiler, it is up to you. And, as long as you use const, calculation or variable, there is no penalty since it is done at compile time and not stored in RAM. If you use a standard global or automatic variable instead of a const, it will use RAM.

Or, you could do like they do with string char arrays, and put a null terminator (or other special character, if you need to be able to have a 0 as an element in the array) at the end and do a strlen() like function that counts until it gets to the null.

Or do like standard PASCAL strings and have the first array element be the array length.