2D Array Declaration Error

[u]Reposting in correct section - Mods please delete original post in "installation and Troubleshooting"[/u]

Hello Everyone

Just trying to learn 2D Arrays and failing!!!

My intent is to have a variable length Array of 8 bytes.

I entered this and it failed compilation

char row = 0;

char RomID[row][8];


void setup() {
  // put your setup code here, to run once:

}

void loop() {
  // put your main code here, to run repeatedly:

}

The error is this "exit status 1 array bound is not an integer constant before ']' token"

Thanks

Why is row a char and not an integer and even if it were an integer why would you want an array with zero rows ?

What exactly do you want to put in the array ?

Hi UKHeliBob

Thanks for the reply

As general point I want to use as little memory as possible and use pointers down the road

"int" take 2 bytes of memory when declared and "char" only takes one, is this different in the Arduino IDE?

With regards your point concerning zero array size - Arrays start at zero and count up. If I put a 1 here I would be reserving twice the memory I need for 1 8byte device.

Ultimately I am implementing a hardwire onewire search routine for 100's of devices and storing their addresses.

"int" take 2 bytes of memory when declared and "char" only takes one, is this different in the Arduino IDE?

An int for most Arduinos is 2 bytes and char is 1

char RomID[0][8];

would declare an array with zero rows. You have to know how many rows there will be in the array when it is declared. You cannot sensibly add rows to an array that has been declared

Are you sure that you need a 2D array ?

char * names[8];  //an array that can hold 8 names

void setup()
{
  Serial.begin(115200);
  while (!Serial);
  names[0] = "Tom";
  names[5] = "Dick";
  names[7] = "Harry";
  Serial.println(names[0]);
  Serial.println(names[5]);
  Serial.println(names[7]);
}

void loop()
{
}

One wire ROM IDs are 8 bytes. So, I’d recommend:

uint8_t RomID[?][8];

@Docara
As already mentioned, the size of arrays like this must be specified at compile time**. So, just make it as big as you’re ever going t need. For example 10 addresses:

uint8_t RomID[10][8];

A typedef (as in the Dallas Temperature library) could make things a little easier:

typedef uint8_t DeviceAddress[8];
DeviceAddress RomID[10];

** Actually, it could be done at run time using dynamic allocation. But, I sense from your question that you’re not ready for that.

Yeah the dynamic allocation is ultimately my desired route.

You are quite right I was thinking of uint8_t - I stand corrected!!

God! ASM is so much simpler sometimes without all these declarations etc LOL

I can see pointers and malloc() etc being used with Atmel Studio is going to be my way to go, I thought Arduino might have made things easier somehow

Thanks again

There is usually little point in dynamic allocation on an Arduino for things like that. There's a finite number of addresses that you have enough RAM for so you might as well set aside that space up front. It doesn't matter that you would be "wasting" it because there's nothing else to use it anyway. Also, using pointers means that you need extra space to store them, so it will likely reduce your potential maximum count.