char array

Hi guys!
Need help.
I want to set the zxcvb array from the val variable.
Use this code:

char val = "fsdfsdfddfd";
char* zxcvb[] = {"xml=<ya_lbs_request><common><version>1.0</version><api_key>",
"AKRzO5i90FDGtfxvxvvfgfd54444444446gfdggfdggfgfg",
"</api_key></common>", 
"jjgfdfghfg", 
"h", 
"g", 
"fgfdg", 
"d", 
"sgfdgfg", 
"a"};


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

for (int i = 0; i < 8; i ++) {
  Serial.println(zxcvb[i]); 
  Serial.println("");
  [b]zxcvb[i] = val;[/b]
  Serial.println(zxcvb[i]);  
}


}


void loop() {

}

i get error, like this:


</api_key>


jjgfdfghfg


h


g


fgfdg


d

What it is ? What is the correct way?

You can't just use an equality sign to copy strings, you will need to use strcpy or equivalent.

Please go to the preferences in the IDE and enable all warnings.

The compiler will tell you what’s wrong:

~/sketch.ino:2:12: warning: invalid conversion from 'const char*' to 'char' [-fpermissive]
 char val = "fsdfsdfddfd";
            ^~~~~~~~~~~~~
~/sketch.ino:12:4: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
 "a"};
    ^
~/sketch.ino:12:4: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
~/sketch.ino:12:4: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
~/sketch.ino:12:4: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
~/sketch.ino:12:4: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
~/sketch.ino:12:4: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
~/sketch.ino:12:4: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
~/sketch.ino:12:4: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
~/sketch.ino:12:4: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
~/sketch.ino:12:4: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
~/sketch.ino: In function 'void setup()':
~/sketch.ino:21:14: warning: invalid conversion from 'char' to 'char*' [-fpermissive]
   zxcvb[i] = val;
              ^~~

These should be errors, really, but unfortunately, Arduino compiles your sketches using the -fpermissive flag.

To fix your problems, use a pointer to read-only characters (const char *) to save your strings, char and char * are both incorrect:

const char *val = "fsdfsdfddfd";
const char *zxcvb[] = {
  "xml=<ya_lbs_request><common><version>1.0</version><api_key>",
  "AKRzO5i90FDGtfxvxvvfgfd54444444446gfdggfdggfgfg",
  "</api_key></common>",
  "jjgfdfghfg",
  "h",
  "g",
  "fgfdg",
  "d",
  "sgfdgfg",
  "a",
};

void setup() {
  Serial.begin(115200);

  for (int i = 0; i < 8; i ++) {
    Serial.println(zxcvb[i]);
    Serial.println("");
    zxcvb[i] = val;
    Serial.println(zxcvb[i]);
  }
}

void loop() {}

countrypaul:
You can’t just use an equality sign to copy strings, you will need to use strcpy or equivalent.

You don’t have to (and are not allowed to) use strcpy here. The array contains pointers to string literals in static memory, not the actual strings.

Pieter

PieterP:
Please go to the preferences in the IDE and enable all warnings.

The compiler will tell you what’s wrong:

~/sketch.ino:2:12: warning: invalid conversion from 'const char*' to 'char' [-fpermissive]

char val = “fsdfsdfddfd”;
            ^~~~~~~~~~~~~
~/sketch.ino:12:4: warning: ISO C++ forbids converting a string constant to ‘char*’ [-Wwrite-strings]
“a”};
    ^
~/sketch.ino:12:4: warning: ISO C++ forbids converting a string constant to ‘char*’ [-Wwrite-strings]
~/sketch.ino:12:4: warning: ISO C++ forbids converting a string constant to ‘char*’ [-Wwrite-strings]
~/sketch.ino:12:4: warning: ISO C++ forbids converting a string constant to ‘char*’ [-Wwrite-strings]
~/sketch.ino:12:4: warning: ISO C++ forbids converting a string constant to ‘char*’ [-Wwrite-strings]
~/sketch.ino:12:4: warning: ISO C++ forbids converting a string constant to ‘char*’ [-Wwrite-strings]
~/sketch.ino:12:4: warning: ISO C++ forbids converting a string constant to ‘char*’ [-Wwrite-strings]
~/sketch.ino:12:4: warning: ISO C++ forbids converting a string constant to ‘char*’ [-Wwrite-strings]
~/sketch.ino:12:4: warning: ISO C++ forbids converting a string constant to ‘char*’ [-Wwrite-strings]
~/sketch.ino:12:4: warning: ISO C++ forbids converting a string constant to ‘char*’ [-Wwrite-strings]
~/sketch.ino: In function ‘void setup()’:
~/sketch.ino:21:14: warning: invalid conversion from ‘char’ to ‘char*’ [-fpermissive]
  zxcvb[i] = val;
              ^~~



These should be errors, really, but unfortunately, Arduino compiles your sketches using the `-fpermissive` flag.

To fix your problems, use a pointer to read-only characters (const char *) to save your strings, `char` and `char *` are both incorrect:


const char *val = “fsdfsdfddfd”;
const char *zxcvb = {
  “xml=<ya_lbs_request>1.0<api_key>”,
  “AKRzO5i90FDGtfxvxvvfgfd54444444446gfdggfdggfgfg”,
  “</api_key>”,
  “jjgfdfghfg”,
  “h”,
  “g”,
  “fgfdg”,
  “d”,
  “sgfdgfg”,
  “a”,
};

void setup() {
  Serial.begin(115200);

for (int i = 0; i < 8; i ++) {
    Serial.println(zxcvb[i]);
    Serial.println("");
    zxcvb[i] = val;
    Serial.println(zxcvb[i]);
  }
}

void loop() {}



You don't have to (and are not allowed to) use strcpy here. The array contains pointers to string literals in static memory, not the actual strings.

Pieter

const char not working? i get incorrect sumbols…

need * sumbol? this working: char* val = "fsdfsdfddfd";

uvarovan2:
const char not working? i get incorrect sumbols…

Please read my previous reply carefully: you need “const char *”, not “const char”.

uvarovan2:
need * sumbol? this working:
char* val = “fsdfsdfddfd”;

No, “char *” is incorrect, you should use “const char *”. The compiler should have warned you about that.

ОК, PieterP, i want array of string, what your advice?

const char * const array[] = {"abc", "defg", "Hello World"};
const char * const array[] = {"abc", "defg", "Hello World"};

const times 2 ?

UKHeliBob: const char * const array[] = {"abc", "defg", "Hello World"};

const times 2 ?

Yes. See: https://stackoverflow.com/questions/1143262/what-is-the-difference-between-const-int-const-int-const-and-int-const

uvarovan2: ОК, PieterP, i want array of string, what your advice?

I already posted the necessary code in my first post.

gfvalvo: const char * const array[] = {"abc", "defg", "Hello World"};

You probably don't want the pointers to be const, though, in his code, OP reassigns the strings in the array.

PieterP: You probably don't want the pointers to be const, though, in his code, OP reassigns the strings in the array.

Perhaps I'm being over-cautious, but I try not to modify pointers that are pointing to string literals. Always concerned about stranding them in memory with no way to access them after.

gfvalvo: Perhaps I'm being over-cautious, but I try not to modify pointers that are pointing to string literals. Always concerned about stranding them in memory with no way to access them after.

That's a valid concern, and good practice in my opinion, but that'll require the OP to change the approach he uses in his original code.

I want to set the zxcvb array from the val variable.

...then why do you initialize it? You'll never be able to access the original contents again. Please explain what is really going on here.