2

I'm trying to manipulate arrays, but I'm a little confused with I need to do. I want to do something like:

char myArray[10];

myArray[0] = 0xA9;
myArray[1] = 'D';
myArray[2] = 'S';
myArray[3] = "qwert";
myArray[9] = 'C';
myArray[10] = '\0';

String fullArray = String(myArray);

Of course, this doesn't work, but I want something like this and if possible manipulate one of the arrays. Is that possible?

I tried this, but I can't output the whole string at once.

char* myStrings[] =
    {"This is string 1",
     "This is string 2",
     "This is string 3",
     "This is string 4",
     "This is string 5",
     "This is string 6"};

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

void loop(){
    for (int i = 0; i < 6; i++){
        Serial.println(myStrings[i]);
        delay(500);
    }
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
mafap
  • 371
  • 3
  • 18
  • 40
  • The Arduino language is so low level that there's no reason you should want to do this at all. An array defines a map to a sequence of variables of the same type and size, making access to any of them instant. You can't have array elements of different sizes. – Greg May 18 '13 at 12:54
  • If you are really desperate, you could go and allocate all the elements in heap memory and have an array of `void *` pointers to them, and have fun trying to figure out what was which type and how big, but I wouldn't recommend that. – Greg May 18 '13 at 12:57
  • 4
    @PartiallyFinite There's no such thing as "the Arduino language". There is assembler, C and C++ in which the AVR in the Arduino is programmed, and there are the Arduino libraries which provide C functions and C++ classes to work with the device. –  May 18 '13 at 13:00
  • @mafap Is all you want is the concatenation of a few strings? –  May 18 '13 at 13:00
  • @H2CO3 I know there's "Arduino language" as such, but technically the "Arduino language" is the *language in which you program the Arduino*. That doesn't necessarily make it separate or different from C++, although it does have some minor differences. – Greg May 18 '13 at 13:03
  • 1
    you need more explanation of "it doesnt display the whole string at once" – SwiftMango May 18 '13 at 13:06
  • @H2CO3 there is arduino language. it is an alternate of C. – SwiftMango May 18 '13 at 13:07
  • 4
    @texasbruce Not quite. It's just normal C. And you can have some AVR- and Arduino-specific libs. I think you might be confusing this with the Processing language which largely influenced the designs of the Arduino. Processing is a proper, stand-alone programming language, similar to C, C++ and Java. The Arduino is **not** programmed using Processing. –  May 18 '13 at 13:10
  • @H2CO3 Thats something like cocatenation. One of the arrays is what I type in keyboard (Serial.read). So variable size if it could be – mafap May 18 '13 at 13:35
  • @H2CO3 http://arduino.cc/en/Reference/Comparison – SwiftMango May 18 '13 at 13:58

1 Answers1

0

Your problem is because you are defining an array of chars, not an array of arrays of chars (i.e. strings).

However:

Don't use char*. Use String. It is much easier to manipulate, and you don't have to worry about pointers/raw values.

E.g.:

String myStrings[3] = {"a", "b", "c"};

for (int i = 0; i < 3; i++) {
    Serial.print(myStrings[i]);
}

Output:

abc
cortices
  • 373
  • 2
  • 15
  • Judging by the question, he also wanted to easily concatenate all the strings in the array to get one big one. – Greg May 18 '13 at 13:06
  • If I do println I get: `a \n` `b \n` `c \n`. And I want `abc` – mafap May 18 '13 at 13:38
  • 1
    @mafap println is short for "print line". You want to use "print" if you do not want a new line character appended. – Kitsune May 18 '13 at 17:13
  • @Kitsune I just wanted to say that if I print myStrings[3] I will print 3 different strings and not just one like I want, or am I wrong? – mafap May 18 '13 at 19:22
  • @mafap Show us an example of the input, as well as the sort of output you want. If for `{"a", "b", "c"}` you want `abc` as output, then all you need to do is, in your second piece of code, change `Serial.println(myStrings[i]);` to `Serial.print(myStrings[i]);`, which won't add in the line breaks. – Kitsune May 18 '13 at 19:52
  • Look, @mafap, go and read the Arduino reference (http://arduino.cc/en/Reference/HomePage). Your question is not clear, though I'm sure you would know the answer if you understood the language. Go and read and play with the tutorials: http://arduino.cc/en/Tutorial/HomePage. – cortices May 19 '13 at 08:31
  • @iamthepiguy that's because of have read all this reference and didn´t know that I'm here... my axample is in array's tutorial but that's not clear enough – mafap May 19 '13 at 11:47
  • @Kitsune I want something like the output of `Serial.print(myStrings[i]);` but I want this to be like a packet, send all the data together. If you add a delay after this serial print you can see that this prints separeted strings and I want just one. – mafap May 19 '13 at 11:51
  • something like [this](http://stackoverflow.com/questions/5697047/convert-serial-read-into-a-useable-string-using-arduino) but between some characters that I can change – mafap May 19 '13 at 11:53
  • @mafap there is no such thing as a packet like that. You cannot be guaranteed chars will be sent with no delay, whatever way they are sent. You need to explain to us what your whole program is trying to do, not just 'manipulate arrays'. Do you want to send a series of strings to another device between delimiting characters, and receive strings back? – cortices May 20 '13 at 22:59
  • @iamthepiguy I want 3 arduinos to comunicate. So I need a frame to send and receive. I thought in something like [StartByte][DestinationAddress][SourceAdress][Data][Checksum]. And I want to know a solution to manipulate this string. I know there are a lot of examples of protocols but I didn´t find anything useful to me because I need to use TX1 and RX1 and my the other things doesnt work for what I need in the future – mafap May 21 '13 at 14:15