Reputation: 877
code:
void setup(){
Serial.begin(19200);
static char value [20] = "20:17:12";
setSimTime(value);
}
void setSimTime(char* incoming){
char dateTime[20];
strcat(dateTime, "20");
strcat(dateTime, incoming[0-1]);
Serial.println(dateTime);
}
What is the right way to do this? Any help is appreciated
Upvotes: 1
Views: 644
Reputation: 1713
The exact error message comes from the fact that incoming[0-1]
is of type char
, but strcat
expects a const char*
as second parameter, which is a pointer to a char
.
The additional const
is not a problem here, because non-const values can always be converted to const values.
See other answers/comments on the invalidity of incoming[0-1]
.
Upvotes: 1
Reputation: 7017
Using strcat(dateTime, incoming[0-1]);
, where incoming[0-1]
is the same as incoming[-1]
causes undefined behavior. In fact incoming[-1]
is the single character located at array index -1
, this is the cause of the error, the compiler cannot convert a single character (char
) into an char pointer (char*
).
Instead use:
strncat(dateTime, incoming, 2);
note the n
in strncat
. This will concatenate at most two characters from incoming
to dateTime
, and put a terminating null-character.
Extension
You can extend this to:
strncat(dateTime, incoming + n, l);
Where n
is the starting index, i.e., zero before, and l
is the max length to copy.
Example
So if you what to concatenate the characters with indexes 3 and 4 do:
strncat(dateTime, incoming + 3, 2);
Upvotes: 1