length() function help

Hello! I am working on writing a function to return the length of any array input to the function.
I have looked around a bit for solutions to my problem, but I have not been able to find a satisfactory solution. I think that the problem has to do with sending the mygrades array to the length function, I think it sends a pointer instead of the physical array. Here is the code I have right now.

int mygrades[]={98,89,94,85,76,94,68,97,87,88,91,94};   //list of my grade with 12 entries
int l;

int length(int array[]){            //length function that I want to return the length (number of entries) of the array input
  int r;
  r=sizeof(array)/sizeof(int);
  return(r);
}

int addition(int a, int b){       //small addition test function
  int r;
  r = a + b;
  return (r);
}

void setup() {
  Serial.begin(9600);
}

void loop() {
 Serial.println(addition(3,5));     //use of the small addition test function   returns 8 as expected
 delay(20)
 Serial.println(length(mygrades));    //use of my length function   returns 1 instead of 12 as expected
 delay(20)
  l = sizeof(mygrades)/sizeof(int);   //the same code used in my length function     
 Serial.println(l);                 //returns 12 the correct length of the array
 delay(2000);
}

Below is another structure for the length function I thought might work, however when I add this function to my code it does not run at all. Do arrays have a null character at the end in the same way that strings do? Did I use the null character correctly?

int length2(int array[]){
  int r=0;
  while(array[r]!='/o'){
    r++;
  }
return(r);
}

Thanks!

I think it sends a pointer instead of the physical array.

That's exactly what it does. You have two choices. You can store a special marker in the array, after the last valid value, like a string with a NULL. Or, you can determine the size of the array where the array is an array, and pass that length along with the pointer to the array.

The problem with the special marker is choosing one that is not a valid value. NULL is easy, because no character has 0 as it's ASCII value.

In your example, mygrades could contain a -1 to mark the last grade. It's unlikely that a negative score is possible.

Getting the size where the array (as an array) is in scope is easier, though.

Note, though, that the size of an array and the number of valid values stored in that array are not the same thing.

int myScore[100];
myScores[0] = 98;

The array size is 200 (100 * 2 bytes per int). There is only one valid value stored.

The first thing to note is that your example code does not compile because of missing semi colons after a couple of the calls to the delay function. Why are the calls there anyway ?

Secondly, if the function is to

return the length of any array input to the function.

how are you going to pass the type of the array to the function ?

Thirdly, you cannot use your second technique of looking for ‘\0’ unless you know that it will be in the last position in the array. In general this will only be the case for a string either defined explicitly or built from input, possibly via Serial, to which you have explicitly added the ‘\0’

Last but not least, I believe that you are right in your conclusion that a pointer to the array is passed to the function.

First of all, Thanks soo much for your responses! Very timely as well XD

PaulS:
Or, you can determine the size of the array where the array is an array, and pass that length along with the pointer to the array.

I am a little confused on what you mean by this. I'm not sure what you mean by "pass that length along with the pointer to the array", why would I be passing something to the array?

UKHeliBob:
The first thing to note is that your example code does not compile because of missing semi colons after a couple of the calls to the delay function. Why are the calls there anyway ?

I went ahead and fixed this. The delays were for my piece of mind, I like to have the program print a value then have a small delay, print a value have a small delay, etc. Instead of just having it print a chunk of numbers all at once.

UKHeliBob:
Secondly, if the function is to

return the length of any array input to the function.

how are you going to pass the type of the array to the function ?

Thank you for pointing this out. I suppose what meant to say was, "return the length of any integer array input to the function." Haha,this opens up a whole new problem for me to work out.

I'm not sure what you mean by "pass that length along with the pointer to the array", why would I be passing something to the array?

I meant that you would pass the pointer to the array and the length of the array to the function that needed to use the array.

nsa10124:
Below is another structure for the length function I thought might work, however when I add this function to my code it does not run at all. Do arrays have a null character at the end in the same way that strings do? Did I use the null character correctly?

Unless you add one, an array won't have a null terminator. Also, your line to check for a null terminator is wrong, it should be:

while(array[r]!='\0'){ //it is '\0' not '/o'

Second thing is that if you want the length of an array, then it is simply:

#define arrayLength(a) (sizeof(a)/sizeof(a[0]))
...
char someArray[20];
byte length = arrayLength(someArray); //length would =20

This works assuming you are working with the declared array pointer and not a pointer to the array which you have passed to a function.
The reason this works is that the compiler (not the avr processor) knows how big the array is at compile time and replaces sizeof() with a literal value. Once you pass the array pointer to another function, the compiler can no longer know the size of the array in the function and so it falls back to using just the size of the pointer itself (2bytes in an avr).

The simplest solution would be to mark the end of the array with -1. Presumably no 'grade is negative. I'm surprised by the amount of discussion on this post rather than giving a simple solution. Remember the KISS principle.

I'm surprised by the amount of discussion on this post rather than giving a simple solution.

Perhaps a closer reading of Reply #1 is in order.

Perhaps a closer reading of Reply #1 is in order.

Your are correct. I failed to notice the -1 marker was already mentioned. All the other responses, including mine, seem surperflous. I still suggest the KISS priciple.

PaulS has 39292 posts! Amazing. How many months does that represent at nn responses per month?

How many months does that represent at nn responses per month?

You can look at the Members section, and see when any person joined. Since I average 20+ posts per day, you can figure out roughly when I joined. Or, go look.

If you click where it says "PaulS" on the left, it goes to his profile which says:

Posts: 39305 (27.448 per day)

Just multiply by 30.4375 to get average posts per month.