Pages: [1]   Go Down
Author Topic: [SOLVED]Getting array lenght inside a function  (Read 418 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 40
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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:
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?

« Last Edit: October 10, 2013, 03:22:35 pm by Tuppe » Logged

Offline Offline
Edison Member
*
Karma: 33
Posts: 1446
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 217
Posts: 13705
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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)
Logged

Rob Tillaart

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

Offline Offline
Edison Member
*
Karma: 33
Posts: 1446
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

France
Offline Offline
Edison Member
*
Karma: 37
Posts: 1008
Scientia potentia est.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Sorry I misunderstood. I thought he wanted a function that returned the # of elements in an array.
« Last Edit: October 10, 2013, 01:08:34 pm by guix » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 40
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Edison Member
*
Karma: 33
Posts: 1446
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
God Member
*****
Karma: 25
Posts: 526
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Pages: [1]   Go Up
Jump to: