char *readByteArray() {
unsigned char l = readByte (); // reads one byte from the stream
char ret[l + 1]; // this should not be done
ret[0] = l; // or could also define a struct for this like {int length, char *data}
readBytes((char *)&ret + 1, l);
return (char *)&ret;
}
So the problem is, that I want to return an array, and the length of the array is determined by the function.
A better example of this would be the function I use for reading a string:
char *readString () {
unsigned char l = readByte (); // reads one byte from the stream
char ret[l + 1]; // +1 for null byte
readBytes((char *)&ret, l); // reads l bytes from the stream
ret[l] = '\0'; // null byte
return (char *)&ret; // problem
}
If the length of the array would be determined before the function I could allocate the array outside the function and pass it as a parameter, but calling this:
unsigned char l = readByte ();
char ret[l + 1];
readString (&ret, l);
every time I want to read a string would kind of defeat the purpose of the function.
Is there an elegant solution for this on windows AND ATmega328 (STL is not available)?