Variable Array

I have a function that uses an array, but it could be passes two different arrays (different sizes and different values) to use. The array the function uses is called "gauge". I thought to do something as simple as this before calling the function:


But that doesn't seem to work. How can I pass the function a different array when I call it separate times?

You make a function parameter that tells the compiler it is an array.

void setup()
void loop()
    int i1[2];
    i1[0] = 1;
    i1[1] = 2;
    int i2[3];
    i2[0] = 11;
    i2[1] = 22;
    i2[2] = 33;

    Serial.println("Calling printIarray(i1,2):");

    Serial.println("Calling printIarray(i2,3)");

    Serial.println("Calling printIarray(i2,2)");
    printIarray(i2,2);    delay(5000);

void printIarray(int a[], int size)
    int i;
    for (i = 0; i < size; i++) {
        Serial.print("    ");
        Serial.print(": ");


Calling printIarray(i1,2):
    0: 1
    1: 2

Calling printIarray(i2,3)
    0: 11
    1: 22
    2: 33

Calling printIarray(i2,2)
    0: 11
    1: 22

Note that there is no way (and I really mean it: no way) that the function can know the size of the array unless you tell it. Really. The usually recommended way is to give the function another parameter to be used as the size. See Footnote.



If the array is an array of chars that you are going to use some “string” stuff with, the “string” size can be inferred within the function without explicitly telling it the size of the array, but there is no way for the function to know what the array size is (unless you tell it).

I shall see if that works. Is there a way to do it without the extra function though? I had originally thought it was just a syntax error, but it might be more. Here is some more information that might clear things up.

int tempgaugeSize=97;
int tempgauge[]={160,158,157,156,155,153,152,151,149,148,147,146,144,142,140,138,137,136,135,133,131,129,128,126,125,122,121,119,117,115,114,112,110,109,107,106,104,103,100,99,97,96,94,92,90,88,86,84,82,81,80,77,76,74,73,70,68,67,66,64,62,59,58,56,55,53,51,49,47,44,43,41,40,38,35,33,31,30,29,28,26,24,22,20,18,17,15,14,12,11,9,7,6,5,4,3,0};
int lmpgaugeSize=103;
int lmpgauge[]={160,159,158,157,156,155,154,153,152,151,149,148,147,146,145,143,142,140,139,138,136,135,134,133,132,130,128,127,126,125,123,121,120,118,117,116,115,112,110,109,107,105,104,103,101,100,100,99,97,94,91,88,87,85,84,82,81,79,78,77,74,72,71,70,67,66,65,62,61,60,59,56,55,53,52,49,47,46,44,41,40,39,36,35,34,32,29,28,26,24,22,20,19,17,16,13,12,9,8,7,6,3,0};
int gaugeSize;
int gauge[]={};

Those are my initialized variables at the beginning of the code. The arrays may change based on some hardware work I do, but it is easy enough to paste in the new values and change the size here if needed.

Later on, i want to be able to call it before a function that uses the variable "gauge" and "gaugesSize":


But, unsurprisingly, I receive the compile error

 In function 'void loop()':
error: incompatible types in assignment of 'int [97]' to 'int [0]'

Is there a way to re-initialize the variable "gauge" so that I can just equate them?

Looking at your code it would appear to work, but I was hoping there might be a simpler solution.


Try this:

int tempgauge[] = {160, blahblahblah};
int lmpgauge[] = {160, blahblahblah};
int *gauge;

gauge = lmpguage;

Of course, fill in the correct numbers in place of "blahblahblah". I also left out the sizes, but you are already doing that correctly.

By the way, you can have the compiler calculate the sizes for you like this:int tempgaugeSize  = sizeof(tempgauge) / sizeof(int);You'll have to put the size declarations after the array declarations, though.



Well it compiled, so that is a start. I'll do some testing on it and make sure it works properly. I have two questions in the meantime:

int *gauge

What does this syntax mean? I've seen it in examples before, but Arduino is really my first C/C++ venture and I'm not sure what its for.


Why is this needed? What is "int" in this?

Thanks so much for your help so far.

"int *p;" declares a pointer called "p", meant for pointing at variables of the type "int". An unitialised pointer is a dangerous thing, so it is best to assign it a value:

int myVariable;
int *p = &myVariable;

Now, "p" points to "myVariable" (the & operator returns the address of the variable). So to access "myVariable" via "p", we can write

*p = 12;

which has exactly the same effect as if we had writtenmyVariable=12;

On the Arduino, an "int" occupies two bytes.

"sizeof" returns the number of bytes the given datatype occupies, so "sizeof(int)" gives you the value 2.

int tempgaugeSize  = sizeof(tempgauge) / sizeof(int);

"sizeof (tempgauge)" gives you the number of bytes occupied by "tempgauge", but the elements of "tempgauge" are "ints", so dividing them gives the number of elements in "tempgauge".

A better way of writng this is int tempgaugeSize  = sizeof(tempgauge) / sizeof(tempgauge [0]);, so even if the type of the elements changes for instance, you decide to make it a "long"), you don't have to trawl through all your code changing all the occurrences.

Note, however, this is a compile-time only technique - it won't give you the number of elements in an array you've passed to a function via a pointer.


The lineint *gauge;says that the variable 'gauge' is a pointer to integers. When you assign tempgauge to it, for example, 'gauge' now points to the beginning of the tempgauge array of integers.

Just about any sufficiently advanced C language tutorial will teach you about pointers, which are very important in C.

The keyword 'sizeof' is a feature that returns the number of bytes that something takes up. You can pass in the name of an array or structure, as in 'sizeof(tempgauge)' or you can pass in a data type, like 'sizeof(int)'. One of the things that's special about sizeof is that it is calculated when the program is compiled, so you can use it in place of hard-coded numbers.

Let's say you have an array of int's

int a[10];
int a_size = sizeof(a) / sizeof(int);

In this case, sizeof(a) is going to return 20, since each int (on the Arduino) takes up two bytes. If you want to know how many elements are in the array, you need to divide it by the size of each element. In this case, sizeof(int) will return 2, and 20 divided by 2 equals 10.



Ok both of those make sense. I have a vague understanding of pointers, but I'll have to take a more in depth look at them.

Thanks so much for your help.