Go Down

Topic: [SOLVED]Getting array lenght inside a function (Read 508 times) previous topic - next topic

Tuppe

Oct 10, 2013, 07:23 pm Last Edit: Oct 10, 2013, 10:22 pm by Tuppe Reason: 1
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:

Code: [Select]
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:
Code: [Select]

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?


KeithRB

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.

robtillaart

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)
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

KeithRB

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

guix

#4
Oct 10, 2013, 08:06 pm Last Edit: Oct 10, 2013, 08:08 pm by guix Reason: 1
Sorry I misunderstood. I thought he wanted a function that returned the # of elements in an array.

Tuppe

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.

KeithRB

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.

TanHadron

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.

Go Up