How can I store a const char* value to char array?

I am wanting to create a temporary list of input switches that are On in an array named onList[12], sourced by swInput[] array with 10-12 items. When the switch is turned off, the onList[] will be updated to remove the On item.

const char* swInput[]={"Sw 10", "Sw 11", "Sw 12", "Sw 13", "Sw 14", "Sw 15", "Sw 16", "Sw 17", "Sw 18", "Sw 19", "Sw 20", "Sw 21"};
char onList[13];

But, when I have a selected array value like swInput[i] (i has a 'i' value from an I/O board read loop, say 3 for example), using the following code, nothing stores in the onList[] array. Serial.print of the swInput[i] value is fine.

  Serial.print(swInput[i]);   //  prints correct value, i=3: "Sw 13"
       onList[i] = swInput[i];  // fails to store to onList[] using active 'i' value
            Serial.print(" is ON,  ");  // prints correctly
       Serial.println(onList[i]);  // prints odd special character

I have searched for a method to do this exact thing but finding nothing helpful.
any advice appreciated.

strcpy(onList[i], swInput[i]);  //copy the string 

You can't strcpy a c-string into one element of a char array.

Your swInput[] is array of pointers to strings, but onList[] is array of chars. It can't be copying one to another

You can copy only one element of swInput array to whole onList

strcpy(onList, swInput[i]);  //copy the string 

So using:

strcpy(onList[i], swInput[i]);  //copy this string

I just wanted to see what happens, causes crash and stop.

Using the above, the stored value won't have an index [i] value looks like.

So I should be considering storing On list in a text file(micro SD card) or EEPROM?

As noted by @gfvalvo my suggested solution was wrong

Your onList array can store single string only, so string indexes are not applicable for it.

I don't understand your idea.

const char* swInput[]={

This is an array of pointers to characters. What the compiler will do is store all those strings in memory somewhere. The contents of swinput[] are not the strings, but rather the addresses of where each string can be found in memory.

char onList[13];

This is an array of characters. It can hold up to 12 characters plus the required trailing null character. It can effectively hold one, and only one, character string. If you try to put a pointer to a string into that array, the compiler may allow it, but the result is almost certain to be unprintable.

Do you really need to store the string for each switch, or just the status of the switches? You could have an array of integers, one for each switch. Set the value for each element to zero when the switch is off, and something other than zero when the switch is on.
Doing it this way, you could then do something like this:

for (int i=0;i<12;i++){
    if (swstatus[i] != 0) {
        Serial.print(swInput[i]);
        Serial.println(" is ON")
    }
}
1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.