Error: variable-sized array may not be initialized

Hello,
I want an Arduino to read INTEGERS from the Serial, ie, if one Arduino sends the number 1745, I want the other Arduino to read 1745 and not some arbitrary ASCII nonsense.

Not knowing how to do it after several days of researching, I tried approaching the problem with a smexy smile and simple words.

This is what I've done:

void setup() {
  Serial.begin(9600);
}

void loop () {
  int i = 0;

  while (Serial.available()) {  // while there's something in the serial buffer...
    int serialIn = Serial.read();
    int string[i] = serialIn;  // store the whole string in an array byte-by-byte
    i++;
  }
  
  Serial.print(string, BYTE); // prints the whole string
  Serial.println();  // to make the output neater
  delay(1000);
}

Now, there's an error I can't seem to get rid of:
In function 'void loop()':
error: variable-sized object 'string' may not be initialized

What the -blam!-.
Will someone help me out here? I've never used arrays, so I don't know what I'm doing wrong...

Also, is there a simpler way to do what I want?
I just want one Arduino to send data gathered from a sensor to another Arduino via RS485 [but RS485 is a story for another day]...

Thanks!

PS: In case you haven't figured out, I'm kinda still a noob, so please don't post very complex code. :slight_smile:

PPS:
Might as well just add the code that I based myself on - which DOES work. The problem is that the data read can't be used, because it's just makeshift, ie, if it reads the number 150 I can't use it to (for example) delay 150ms or PWM an LED with a value of 150.
Here it is:

/* ------------------------------------------------
* SERIAL COM - HANDELING MULTIPLE BYTES inside ARDUINO - 01_simple version
* by beltran berrocal
*
* this prog establishes a connection with the pc and waits for it to send him
* a long string of characters like "hello Arduino!".
* Then Arduino informs the pc that it heard the whole sentence
*
* this is the first step for establishing sentence long conversations between arduino and the pc.
* serialRead() reads one byte at a time from the serial buffer.
* so in order to print out the whole sentence at once
* (it is actually still printing one byte at a time but the pc will receive it
* not interupted by newLines or other printString inside you loop)
* You must loop untill there are bytes in the serial buffer and
* and print right away that byte you just read.
* after that the loop can continue it's tasks.
*
* created 15 Decembre 2005;
* copyleft 2005 Progetto25zero1 <http://www.progetto25zero1.com>
*
* --------------------------------------------------- */

int serIn; //var that will hold the bytes in read from the serialBuffer

void setup() {
  Serial.begin(9600);
 
}

//auto go_to_the_line function
//void printNewLine() {
// Serial.print(13, BYTE);
// Serial.print(10, BYTE);
//}


void loop () {
  //simple feedback from Arduino Serial.println("Hello World");
  
  // only if there are bytes in the serial buffer execute the following code
  if(Serial.available()) {
    //inform that Arduino heard you saying something
    Serial.print("Arduino heard you say: ");
    
    //keep reading and printing from serial untill there are bytes in the serial buffer
     while (Serial.available()>0){
        serIn = Serial.read(); //read Serial
        Serial.print(serIn, BYTE); //prints the character just read
     }
     
    //the serial buffer is over just go to the line (or pass your favorite stop char)
    Serial.println();
  }
  
  //slows down the visualization in the terminal
  delay(1000);
}
    int string[i] = serialIn;  // store the whole string in an array byte-by-byte

This is the line that is causing the problem. i changes on each iteration, so the compiler thinks (rightly so) that you are trying to create a new, variable sized array on each pass through loop.

You need to define an array to store the values in:

int string[10]; // Should be big enough

Put this before setup().

Then change

    int string[i] = serialIn;  // store the whole string in an array byte-by-byte

to

    string[i] = serialIn;  // store the whole string in an array byte-by-byte

This will make the compiler happy, but it probably won't solve your problem.

Where is the data coming into the serial port coming from? If it is being sent by the Serial Monitor, is is being sent as "some arbitrary ASCII nonsense". You'll need to deal with that. It's possible, in several ways. But, answer the question, first.

I've done what you've told me to do and a new error occurred:
C:\Program Files (x86)\arduino-0018\hardware\arduino\cores\arduino/Print.h:48: note:

  • void Print::print(long unsigned int, int) *

The data is supposed to come from another Arduino, but I've been testing with the Monitor, ie, writing some numbers and then clicking "Send".
The Transmitting Arduino has this code (which will be heavily modified. I'm just trying to get the reading right, THEN I'll move on to something bigger):

int TXpin = 1;

void setup(){
  Serial.begin(9600);
  pinMode(TXpin, OUTPUT);
}

void loop(){
  int decValue = 1234;
  Serial.println(decValue);
  delay(100);
}

What happens if the read string is shorter than 10 bytes? Is there a problem with that?

I don't think that the println function is overloaded in as many ways as the print function. Try

  Serial.print(decValue);
  Serial.println();

The problem is not in the Transmitter. The problem is in the Receiver (the first code I posted).
The error also says
error: call of overloaded 'print(int [10])' is ambiguous
. Don't ask me why.

  Serial.print(string, BYTE); // prints the whole string

First off, despite the name, string is not a string. It is an array of integers. A string is an array of characters that is NULL terminated.

So, you are trying to print an entire array as a single byte. That is not going to work.

If you are reading characters, change int to char in the declaration of string. Then, get rid of the BYTE argument in the Serial.print call. After each character is added to string, you need to add a NULL.

string[i] = serialIn;
string[i+1] = '\0';

There is no 10 element int array in the first code you posted.
Why don't you post the code that failed to compile?

OK, I've finished it!
Here it is, a Serial Integer Reader.

/*
 * Serial integer reader, by Pedro Tome'
 * Version 0.01
 *
 * Reads integer numbers from the Serial buffer and translates them
 * into the value of the variable "number".
 *
 * Example:
 * The number 3782 is outputted to the Serial buffer.
 * The variable "number" now holds the value 3782.
 *
 * The maximum value is 65535 due to "number" being an unsigned int.
 */


char charArray[6];
int intArray[6];
unsigned int number;


void setup() {
  Serial.begin(9600);
}


void loop () {
  int charIndex = 1;
  int intIndex = 1;
  int max_intIndex;
  number = 0;
  
  /* If there is something in the serial buffer */
  if (Serial.available()){
    
    /* Read the data from Serial and store it in a charArray */
    while (Serial.available()) {  // while there's something in the serial buffer...
      int serialIn = Serial.read();
      delay(5);
      charArray[charIndex] = serialIn;  // store everything that was read in charArray byte-by-byte
      charArray[charIndex+1] = '\0';  // add a NULL value
      charIndex++;
    }
    charIndex = 1;  // Reset charIndex
    
    
    /* Separate the charArray and convert it into an intArray */
    while (charArray[charIndex] != '\0') {  // while the value is not NULL, ie, while there's a relevant value
      max_intIndex = intIndex;
      intArray[intIndex] = charArray[charIndex]-'0';
      charIndex++;
      intIndex++;
    }
    intIndex = 1;  // Reset intIndex
    
    
    /* Build one single number from the intArray */
    for (intIndex = 1; intIndex <= max_intIndex; intIndex++) {
      number = number*10 + intArray[intIndex];
      delay(5);
    }
    
    Serial.print("number: "); Serial.println(number);
    Serial.println();
    delay(10);
  }
}

It only works if you send from the Serial Monitor, though. I've got to work on it to make it work with data sent from other Arduinos, ie, have one Arduino with this code:

int TXpin = 1;

void setup(){
  Serial.begin(9600);
  pinMode(TXpin, OUTPUT);
}

void loop(){
  int decValue = 1234;
  Serial.print(decValue);
  Serial.println();
  delay(1000);
}

and have another Arduino with the Serial Integer Reader code and read the output from the first Arduino (in this case, the number 1234).

Not meaning to be negative, but there are some opportunities for improvement, here.

Array indexes start at 0, not 1.

This code:

    /* Separate the charArray and convert it into an intArray */
    while (charArray[charIndex] != '\0') {  // while the value is not NULL, ie, while there's a relevant value
      max_intIndex = intIndex;
      intArray[intIndex] = charArray[charIndex]-'0';
      charIndex++;
      intIndex++;
    }
    intIndex = 1;  // Reset intIndex
    
    
    /* Build one single number from the intArray */
    for (intIndex = 1; intIndex <= max_intIndex; intIndex++) {
      number = number*10 + intArray[intIndex];
      delay(5);
    }

(which doesn't need any of the delays) is what the atoi function does for you.

number = atoi(charArray);

Critics are ALWAYS welcome!

I know indexes always start at 0, but starting at 1 made things a bit simpler.
atoi() always returns 0 for me.. I tried using it before writing this code but it never worked...

And I have deleted all the delays except for the one after Serial.read() because it takes time to read. I didn't calculate how much, but I figured 5ms would be enough. "What say you?"

The Serial.read() function does not return until the read is complete, so, no delay should be necessary. There have been some cases where a delay was required. 1 millisecond is usually enough.

Have you tried atoi with a properly NULL-terminated array, like you have now?

I tried the atoi function before writing my last post - just to check if it would work. It didn't.
Thanks for clearing that Serial.read() delay thing. I'll delete the delay when I get to the computer with the sketch.

Thanks for your help, man! :slight_smile: