Pages: [1]   Go Down
Author Topic: Declare array of String  (Read 2531 times)
0 Members and 1 Guest are viewing this topic.
italy
Offline Offline
Full Member
***
Karma: 3
Posts: 222
Muuuuu
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
String result[SENSOR_NUMBER];
where SENSOR_NUMBER is a constant. Thanks
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 212
Posts: 13531
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Rob Tillaart

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

italy
Offline Offline
Full Member
***
Karma: 3
Posts: 222
Muuuuu
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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!
Logged

italy
Offline Offline
Full Member
***
Karma: 3
Posts: 222
Muuuuu
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I prefer to use the String type because if not I must use somthing like this:
Code:
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.
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 212
Posts: 13531
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Think you need something like
Code:
#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()
{
}

Logged

Rob Tillaart

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

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 201
Posts: 8685
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If you are using string constants, the compiler can count so you don't have to set aside a fixed amount of space:
Code:
char *pointer[] = { "uno", "due",  "due"};
Logged

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

italy
Offline Offline
Full Member
***
Karma: 3
Posts: 222
Muuuuu
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If you are using string constants, the compiler can count so you don't have to set aside a fixed amount of space:
Code:
char *pointer[] = { "uno", "due",  "due"};

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

New Hampshire
Offline Offline
God Member
*****
Karma: 17
Posts: 781
There are 10 kinds of people, those who know binary, and those who don't.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged


italy
Offline Offline
Full Member
***
Karma: 3
Posts: 222
Muuuuu
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

May be I have understood my problem. The way of declaring the array of string
Code:
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.
Logged

Pages: [1]   Go Up
Jump to: