Error Creating Arrays

Hi. I'm trying create a 2D array, but all the time I have error when I compile the code. The error say

test.ino: In function 'void setup()':
test:71: error: expected primary-expression before '{' token
test:71: error: expected `;' before '{' token
test:93: error: expected `}' at end of input

and the code for create the array is

  int numberSections = 4 ;
  String sections[numberSections][2];
  sections = {{'S', totalSSec},{'B', totalBSec},{'R',totalRSec},{'T',totalTSec}};

Where is the problem because I see different pages for see example of arrays and use the same but without error and now I am a bit confused

I can't see properly, but are those single quotes?

Ups yes. I change for try and I completely forgot to change again for copy the code here.

  int numberSections = 4 ;
  String sections[numberSections][2];
  sections = {{"S", totalSSec},{"B", totalBSec},{"R",totalRSec},{"T",totalTSec}};

So why aren't you initialising the array when you declare it?

Because I have another error. The first time I try to declare the array and assign values to them in the first line and error.

  int numberSections = 4 ;
  String sections[numberSections][2]  = {{"S", totalSSec},{"B", totalBSec},{"R",totalRSec},{"T",totalTSec}};

and error

test.ino: In function 'void setup()':
test:70: error: variable-sized object 'sections' may not be initialized

OK, just post your code.

void setup() { 

  Serial.begin(9600);

  int totalSSec = 324453;
  int totalBSec = 324234;
  int totalRSec = 897645;
  int totalTSec = totalSSec + totalBSec + totalRSec;

  int numberSections = 4 ;
  String sections[numberSections][2]  = {{"S", totalSSec},{"Bike", totalBSec},{"Run",totalRSec},{"Total",totalTSec}};
  
  for (int count = 0; count < 5; count++){
    Serial.println(sections[count][2]);
  }
 
  
}
  int numberSections = 4 ;

You can't initialize with a variable number of elements.

  const int numberSections = 4 ;
 int totalSSec = 324453;
  int totalBSec = 324234;
  int totalRSec = 897645;
  int totalTSec = totalSSec + totalBSec + totalRSec;

  int numberSections = 4 ;
  String sections[numberSections][2]  = {{"S", totalSSec},{"Bike", totalBSec},{"Run",totalRSec},{"Total",totalTSec}};

Why are you putting numbers into String? String is inefficient at the best of times.

I did’nt know that. I am starting learning. I change the code and I make two arrays one for the string and another for the int values.

void setup() { 

  Serial.begin(9600);

  int totalSSec = 1111;
  int totalBSec = 2222;
  int totalRSec = 3333;
  int totalTSec = totalSSec + totalBSec + totalRSec;

  const int numberSections = 4 ;
  String sections[numberSections]  = {"S","B","R","T"};
  int sectionsSec[numberSections] = {totalSSec,totalBSec,totalRSec,totalTSec};
  
  for (int count = 0; count < 4; count++){
    Serial.println();
    Serial.print(sections[count]);
    Serial.print(" is ");
    Serial.print(sectionsSec[count]);
  }
}

First, while the String class (note the capital 'S') brings a lot of functionality to the party, it often costs more memory than it's worth. Usually a simple char array that is null terminated ('\0') is more efficient. Second, if you need an array that has dissimilar data types (what I call arrays for adults), use a structure:

struct Data {
   char myArray[16];
   int myInteger;
}:
struct Data myData[20];

The struct above creates an array of 20 Data structures named myData which you can access with syntax like:

myData[0].myInteger = 10;

This allows you to have a single array, but with different data types as members of that struct array. Note that the max "string" size you can have is 15 characters because you need the null byte at the end of the string. You can also initialize them, but I'll let you discover how to do that.

Jota_sk:
I did’nt know that. I am starting learning.

In that case try to learn using char arrays rather than “string”. It’ll save you some grief in the long run.