Passing arrays to a function

An entirely beginner question, I know, but how do you pass an entire array into a function? The function should take an array of integers and return a single integer.

You simply pass the name of the array.

int Arr[] = { 1, 2, 3 } ;
int Brr[] = { -1, 0, 1 } ;

void setup() {
  Serial.begin(9600) ;
  Serial.println(SumArr(Arr)) ;
  Serial.println(SumArr(Brr)) ;
}

void loop() {
}

int SumArr(int Xrr[3]) {
  int Z = 0 ;
  for ( int n=0; n<3; n++) Z += Xrr[n] ;
  return Z ;
}

If the size of array varies or you do not know its size, then it gets just a tiny amount trickier, but not not much

To clarify, this is known technically as 'call-by-reference' - the array data stays where it is and the function gets given a copy of a pointer to the array. If the function alters elements of the array it alters the one and only copy of the array that exists.

But if the function is just, say, adding all the elements of that array and returning the result, that's no problem, right?

If the size of array varies or you do not know its size, then it gets just a tiny amount trickier, but not not much

Probably not related, but does the sizeof() function applied to a string array return the maximum possible size of the array or the size of a 'enclosed' string up to the null character?

Lefty

The sizeof() function does not care about the contents of an array, so it returns the size of the array.

The strlen() function, on the other hand, will return the position of the first NULL in a char array.

But if the function is just, say, adding all the elements of that array and returning the result, that's no problem, right?

If the function is not (supposed) to alter the array, the argument should be declared const, so the compiler will complain if you do actually try to write to some position in the array.

for multi dimensional arrays (char) i do like so

void method(char* array[])

As mentioned, if you don’t know the size of the array, it is more complicated, because the function doesn’t know the end of the array (it only gets a pointer to the start). So then your function should provide the size as part of its parameters (“int total” here):

int SumArr(int *Xrr,int total)
{
  int Z = 0 ;
  for ( int n=0; n<total; n++) Z += Xrr[n] ;
  return Z ;
}

For an array, an easy way to pass the size is using the sizeof() ‘function’ (it’s actually a macro). It returns the actual bytes used to store an item. For example:

int array[3];
int total=sizeof(array);

total is 6, not 3, since each ‘int’ item takes two bytes of space.

A safe way to get items instead of size is to divide the total by the size of an individual item (here ‘array[0]’):

int totalItems=sizeof(array)/sizeof(array[0]);

The advantage of this is that if you change your code later (say to use an array twice as big) you won’t have to search for this function and change the array size as well - this makes pointers very useful and low maintenance.

thought I'd add some 'language basics' that hopefully clears up any ambiguity.

when you declare an array you allocate storage (and possibly initialize) a block of memory. The name of the array is considered to be of type 'datatype *' where 'datatype' is the type of data stored, typically char or int or long.

So for the following:

const char aszChar[]="some string"; int aiIntArray[10]={0};

'aszChar' is 12 bytes long and contains a null-byte terminated string. 'aiIntArray' is 20 bytes long and contains all zeros (even though only the first was specified).

The variable aiIntArray is treated as 'int *' by the compiler, identical to '&(aiIntArray[0])'

The variable 'aszChar' is treated as 'const char *' by the compiler, identical to '&(ascChar[0])'

If you pass the array name to a function, it will be treated as a pointer argument. If you look at the 'Print' class, you'll see that one version of 'println' accepts a 'char []' data type as an argument. This is literally the same as 'char ' since no array size was declared. If you declared an array of a particular size as an argument, the compiler would at least give you a warning if you passed a pointer to an array of a different size. As for me, I try to use "" rather than '[]' in declaring function arguments, but either one will work the same.

Summary: an array refers to data storage. A pointer is an address to that data storage. A variable 'int *' is a pointer to an integer data type, or (more literally) the memory address of the first element of an array of integers.

I was long ago taught to refer to 'int *' as "int pointer" and "*ptrvar" as "contents of ptrvar". Sometimes that paradigm helps straighten things out. Also, "*ptrvar" is the same as "ptrvar[0]", even if "ptrvar" is declared as a pointer (not an array).

Anyway, I hope this makes things clearer, not worse.