Initializing const int

Hi,

I’m trying to assign an int returned from a function to a const int at the declaration so I can use this const int as one of the sizes of a 3 dimensional array. How is this done?
Here’s my code:

#include <SPI.h>
#include <SD.h>
File Data;
const int  sec=ParamatersSync();          \\The problem
int Move[2][sec][2];
void setup(){}
void loop(){}

int ParamatersSync()
{
 int a=0;
 ConnectSD();
 Data=SD.open("Test.txt");
 while(Data.available())
 if(Data.read()==';')
 a++;
 Data.close();
 return a;
}

void ConnectSD()
{
 Serial.print("Initializing SD card... ");
 pinMode(10,OUTPUT);
 if(!SD.begin(8))
 {
  Serial.println("Failed. Try to restart.");
  return;
 }
 else Serial.println("Succesfull!!!");  
}

I get the following error:

error: array bound is not an integer constant

I’m not trying to change this const int at any part of the code but it’s still an error?

Are their any other ways of assigning a variable to a const int?

Simple. You can't. The array size must be defined by a constant, which means it must be defined at compile-time, not at run time. And you CANNOT assign to a constant, except with a constant initialization value.

Regards,
Ray L.

The whole point of a constant is that it doesn’t change. Also trying to initialize it at global scope has other issues.

I suggest you use malloc (or new) to dynamically allocate your array.

trying to initialize it at global scope has other issues.

Yes, and more specifically, "Could the array be a local variable instead?" If the array is only used for a short period of time, and not across multiple calls to loop(), it could be local. Local variables can have a non-const dimension:

int sec;

void setup()
{
  sec = ParametersSync();
}

void loop()
{
  int Move[2][sec][2];
  //  do some things with "Move"...
}

It's probably better if you stop and analyze whether there is a maximum to this array. Arduinos don't have a lot of RAM, so you have to be careful about unlimited arrays. Is there a max size? If there is, you can certainly dimension it to the max, and just keep track of what you're currently using.

Does the size change, ever? Frequently?

Nick Gammon:
I suggest you use malloc (or new) to dynamically allocate your array.

I'm afraid I have to disagree with this. There are very few cases in an embedded environment where you should use malloc/new, especially if you also use free/delete when the size changes. If you're just tinkering and learning about C++, it's not a big deal. If you eventually want your sketch to run forever, I'd stay away from "dynamic memory" until you've got a few projects under your belt. Using String falls into the same category: tinkering, ok; real project, avoid.

Cheers,
/dev

Dynamic memory allocation certainly has issues as well. Still, malloc/free do work and in this particular example, where the allocation is being done once, I don't see what the huge objection is.

If there is, you can certainly dimension it to the max, and just keep track of what you're currently using.

Since this "const" was being initialized once, globally, I'm inclined to agree you may as well just make it as large as you can. Why not?

What he wanted could not have been done anyway as globals are initialized before control is turned over to 'main' which is where the Arduino API are initialize before 'setup' and 'loop' are called.

Why is that a problem?

He was querying files size on the SD card before the hardware was setup and using the result to set the arrays size, but couldn't because the hardware wasn't yet initialized.

Bad loop there somewhere ...