[solved, under 10 mins ;) ]How to put new contents into a string


with numbers can this, , prints 5 followed 10:

code: [select]
void setup() {
  // put setup code here, run once:
  serial.begin(9600);
  int x = 5;
  serial.println(x);
  x = 10;
  serial.println(x);
}

void loop() {}


i hoped same true string, tried this:

code: [select]
void setup() {
  // put setup code here, run once:
  serial.begin(9600);
  char stringx[]="foo";
  serial.println(stringx);
  stringx[]="bar";
  serial.println(stringx);
}

void loop() {}


it fails compile, highlighting line:

code: [select]
stringx[]="bar";

with message:

code: [select]
expected primary-expression before ']' token

evidently it's not simple replacing int new value.

i can of course, i'm hoping there's simpler way (not including using did here character character):

code: [select]
void setup() {
  // put setup code here, run once:
  serial.begin(9600);
  char stringx[]="foo";
  serial.println(stringx);
  stringx[0]='b';
  stringx[1]='a';
  stringx[2]='r';
  serial.println(stringx);
}

void loop() {}


to recap: there way replace string in way analogous way can overwrite number?

you have use strcpy (or strncpy) function it. example :
code: [select]

  char stringx[]="foo";
  serial.println(stringx);
 //  stringx[]="bar";
  strcpy(stringx,"bar");


Arduino Forum > Using Arduino > Programming Questions > [solved, under 10 mins ;) ]How to put new contents into a string


arduino

Comments

Popular posts from this blog

DHT11 Time out error using v0.4.1library

Sketch upload fails with Java error (___REMOVE___/bin/avrdude)!

Arduino Uno + KTY81/210 temperature sensor