Pages: [1]   Go Down
Author Topic: concatenating date into array  (Read 491 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 10
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi, I'm trying to build up a string that contains a date. 

I have this array:
char msg[80];

I have this time object from <Time.h>
time_t pctime;

I go get the time from the interwebs using magic

then I can Serial.print(day(pctime));

and it works great.  Then I start building up my string:
strcpy(msg, "Day ");

But when I try to put the day in:
  strcat(msg, day(pctime));

I get this message:

error: invalid conversion from 'int' to 'const char*'
error: initializing argument 2 of 'char* strcat(char*, const char*)'

Sadly, my C acumen isn't sharp enough to figure out what any of that means.

Thank you for any insights you may have.

DougM









Logged

California
Offline Offline
Faraday Member
**
Karma: 82
Posts: 3123
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Looks like day() returns an int while strcat() expects a null terminated char array (known as a string). Try sprintf() instead:

Code:
sprintf(msg, "Day %d", day(pctime));
Logged

Offline Offline
Edison Member
*
Karma: 17
Posts: 1041
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
error: invalid conversion from 'int' to 'const char*'
This error message means you have one thing that's an "int" and another that wants a "const char*" and you're trying to put the round "int" peg into to the square "const char*" hole. As arrch said, day() returns an int, and strcat() wants a const char*

An int is a number. A char* is a pointer to a character (or in this case, a pointer to the beginning of a bunch of characters right next to each other, sometimes called an array or a string). A pointer is just a number that means a location in memory, just like a house address. It's easier to tell someone your house address and have them look you up on street view than to give them every dimension in your house, which is why we tell people our house addresses. When you want to pass a lot of data from place to place in a program, you can similarly use a pointer to pass the data quickly. Of course, if you give someone a house, they can do whatever they want with it, but if you tell them the address you're expecting that they not burn it down. This is what the "const" means -- it's a promise that whoever is accepting the pointer won't change the thing it's pointing at (keep in mind that in C, this promise can be broken. But if it's broken for a certain function, you should stop using the program)
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 10
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you both for your responses - that makes much more sense.

I'll try the sprintf route tonight when I get back to it.

DougM

Logged

Pages: [1]   Go Up
Jump to: