Go Down

Topic: Declare array of String (Read 8857 times) previous topic - next topic

cantore

Hello, do you know how can I declare an array of the type String (notice the upper case S).  I want to do something like this:
Code: [Select]
String result[SENSOR_NUMBER]; where SENSOR_NUMBER is a constant. Thanks

robtillaart

Sounds like it should work.
What is the value of SENSOR_NUMBER?

remember Strings and their manipulation are RAM hungry and an Arduino has 2K
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

cantore

Thanks for your reply. SENSOR_NUMBER is an int, I think value can be between 5 and 10 for example. I don't know why but declaring the array of sting in this way all the string are corrupted!

cantore

I prefer to use the String type because if not I must use somthing like this:
Code: [Select]

const int maxSize=10;
char result0[maxSize] = "uno";
char result1[maxSize] = "due";
char result2[maxSize] = "due";
char *pointer[3];

void setup() {
 
  Serial.begin(9600);
  pointer[0]=result0;
  pointer[1]=result1;
  pointer[2]=result2;

}


where maxSize make me lost a lot of memory. This depends on the fact that my strings are not constant and during the program they will be longer than the string declared for first. I also need to access strings in a loop and using this solution I have to adopt a pointer. So if it would be possible to declare a String vector it will be easier.

robtillaart

your question is about Strings and your code example is about char array's. These are different datatypes.

Think you need something like
Code: [Select]

#define ARRAYSIZE 10
String results[ARRAYSIZE] = { "uno", "duo", "tri" };

void setup()
{
  Serial.begin(115200);
  for (int i =0; i< ARRAYSIZE; i++) Serial.println(results[i]);
}

void loop()
{
}


Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

johnwasser

If you are using string constants, the compiler can count so you don't have to set aside a fixed amount of space:
Code: [Select]

char *pointer[] = { "uno", "due",  "due"};

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

cantore


If you are using string constants, the compiler can count so you don't have to set aside a fixed amount of space:
Code: [Select]

char *pointer[] = { "uno", "due",  "due"};



this is possible only if I know the content when I am declaring the string

jraskell


Thanks for your reply. SENSOR_NUMBER is an int, I think value can be between 5 and 10 for example. I don't know why but declaring the array of sting in this way all the string are corrupted!


Show us the code you're using that is corrupting the strings.

cantore

May be I have understood my problem. The way of declaring the array of string
Code: [Select]
String result[NUMBER_OF_SENSORS];
should be correct. The problem of string corruption should be that my sketch seems to use more than 2KB of ram. I need to use an arduino mega for testing the system.

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy