19

I'd like to make three arrays of the same length. According to the documentation, Arrays must be defined as int myArray[10]; where 10 can be substituted for a known length (another integer), or filled with an array {2, 3, 5, 6, 7}.

However, when I attempted to declare a value int arrSize = 10; and then an array based on that size int myArray[arrSize];, I get the following: error: array bound is not an integer constant.

Is there a way to variably determine array sizes, or do I just need to hardcode them? (I was taught hardcoding is bad and something to avoid at all costs.)

user3.1415927
  • 293
  • 1
  • 2
  • 5

5 Answers5

30

Your question has 2 parts actually.

1/ How can I declare the constant size of an array outside the array?

You can either use a macro

#define ARRAY_SIZE 10
...
int myArray[ARRAY_SIZE];

or use a constant

const int ARRAY_SIZE = 10;
...
int myArray[ARRAY_SIZE];

if you initialized the array and you need to know its size then you can do:

int myArray[] = {1, 2, 3, 4, 5};
const int ARRAY_SIZE = sizeof(myArray) / sizeof(int);

the second sizeof is on the type of each element of your array, here int.

2/ How can I have an array which size is dynamic (i.e. not known until runtime)?

For that you will need dynamic allocation, which works on Arduino, but is generally not advised as this can cause the "heap" to become fragmented.

You can do (C way):

// Declaration
int* myArray = 0;
int myArraySize = 0;

// Allocation (let's suppose size contains some value discovered at runtime,
// e.g. obtained from some external source)
if (myArray != 0) {
    myArray = (int*) realloc(myArray, size * sizeof(int));
} else {
    myArray = (int*) malloc(size * sizeof(int));
}

Or (C++ way):

// Declaration
int* myArray = 0;
int myArraySize = 0;

// Allocation (let's suppose size contains some value discovered at runtime,
// e.g. obtained from some external source or through other program logic)
if (myArray != 0) {
    delete [] myArray;
}
myArray = new int [size];

For more about problems with heap fragmentation, you can refer to this question.

jfpoilpret
  • 9,162
  • 7
  • 38
  • 54
2

I know I'm a little late here, but in theory regular arrays can't be created using a variable to define the amount of elements the array is going to have as in:

int arrSize;
int myArray[arrSize];

This will display an error since when declaring the array, the program expects for the value between the brackets to be a constant. Yet, there is a way in which you could create an array with a variable defining the amount of values this arrays is going to have through dynamic memory allocation for value sets (this method has been tested with monodimensional arrays only, haven't tried for multidimensional yet), and it goes something like this:

//First you create a pointer for the memory space to be separated for the set you're creating
int* myArray;
int arrSize; //Then you define the variable that will determine the amount of elements the array is going to have, you can give it a value whenever you want as long as this int is defined before the values in myArray are set 
myArray=(int*)calloc(arrSize,sizeof(int)) //Here, you establish that the instance myArray (whose memory space has already been separated through the creation of the pointer) will be separated into arrSize amount of elements of type int with a maximum memory value (in bytes) equal to the maximum available for the int type variables

After this, all that's left to do is assign a value for every element created in the instance myArray (which is already an Array by now) as you would for a normal array created as myArray[arrSize].

NibboNSX
  • 21
  • 1
1

If you know the maximum length of array, just initialize the array to that length and use an integer to tell the program how much of that array to use. If it's the difference between 7,10 bytes then you are not wasting that much memory allocation.

MichaelT
  • 887
  • 3
  • 8
  • 22
0

The size of the array must be known at compile time. Otherwise you should allocate memory dynamically using:

char *chararray = malloc(sizeof(char)*x);

where x(an integer) can be set in the application code (you could load it from eeprom if you wanted it be a persistent but configurable setting).


However if you just want to declare some arrays of the same size, you just have to declare the number a constant like this:

const int arrsize = 10;
char array1[arrsize];
int array2[arrsize];

I think not hardcoding things only makes sense if you would reasonably expect the user to want to change the setting at some point. I don't know if that's the case.

user2973
  • 1,391
  • 8
  • 12
0

The coderwall explains in Some detail the theory of how best to get the size of an array in C++ https://coderwall.com/p/nb9ngq/better-getting-array-size-in-c

John
  • 9
  • 1