简体   繁体   中英

Working with arrays on Arduino in C++

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);
    }
}

Your problem is because you are defining an array of chars, not an array of arrays of chars (ie 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.

Eg:

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

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

Output:

abc

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM