Go Down

Topic: strncpy error (Read 607 times) previous topic - next topic

robcole

I am attempting to copy characters from a string using strncpy.
I get the following error:
error: cannot convert 'StringSumHelper' to 'const char*' for argument '2' to 'char* strncpy(char*, const char*, size_t)'

My sketch is as follows:

Code: [Select]
String GsmIn;
char GsmBytes;
char deviceID1[9];

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

void loop() {
  while (Serial.available()) {
  delay(10);
    if (Serial.available() >0) {
    GsmBytes = Serial.read();
    GsmIn += GsmBytes;}
  }

if (GsmIn.length() >0) {
          if (GsmIn.startsWith("SW")) {
//          strncpy( deviceID1, GsmIn+2, 8); // This is where I have an error
   
      Serial.println("Got SW"); // Debug to check if I got "SW"
  }
GsmIn=""; // Clear Buffer
}
   }


Could anyone see the problem?

AWOL

Quote
I am attempting to copy characters from a string using strncpy.

Incorrect.
You are attempting to copy characters from a String using strncpy.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

marco_c

Are the String object and the classic C string (nul terminated array of characters) the same? Strcpy copy is for the latter.
Arduino libraries http://arduinocode.codeplex.com<br />Parola for Arduino http://parola.codeplex.com

robcole

Sorry, I don't understand.
The string received is:
SW0013A2004063D9BBD14

dxw00d

What they are telling you is that the strncpy() function operates on c-style strings (null-terminated arrays of chars), not String objects created with the String class. You could achieve the result you are after using a combination of String.substring() and String.toCharArray().

majenko

"C" strings and C++ "Strings" are two very very different things.

A "C" string is a pointer to an array of characters, terminated by a NULL character (ascii(0)).

A C++ "String" is a complete C++ object with many methods and properties etc.

There are ways of converting between the two, and you have to make sure that you are working with the right type of string for the function you are calling.
Get 10% off all 4D Systems TFT screens this month: use discount code MAJENKO10

Go Up