Pages: [1]   Go Down
Author Topic: strncpy error  (Read 510 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 21
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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?
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24299
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I am attempting to copy characters from a string using strncpy.
Incorrect.
You are attempting to copy characters from a String using strncpy.
Logged

"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.

Sydney, Australia
Offline Offline
Edison Member
*
Karma: 27
Posts: 1178
Big things come in large packages
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Are the String object and the classic C string (nul terminated array of characters) the same? Strcpy copy is for the latter.
Logged

Arduino libraries http://arduinocode.codeplex.com
Parola hardware & library http://parola.codeplex.com

Offline Offline
Newbie
*
Karma: 0
Posts: 21
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Gosport, UK
Offline Offline
Faraday Member
**
Karma: 19
Posts: 3114
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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().
Logged

UK
Offline Offline
Faraday Member
**
Karma: 92
Posts: 3969
Where is your SSCCE?!?!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

"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.
Logged

Why not visit my eBay shop? http://stores.ebay.co.uk/Majenko-Technologies
Replacement for the Arduino IDE: UECIDE - Proper serial terminal, graphing facilities, plugins, overhauled internals.
Java isn't bad in itself, but it has enabled morons to write programs.

Pages: [1]   Go Up
Jump to: