Hi,
i´m in the beginning in getting familar with C and struggle with data types.
My example is pretty simple:
I want to retrieve the current date and want to extract a const char* cch_timeFullDate which contains for example: "Samstag, 3. Dezember".
I´m struggeling in getting a single variable of const char* and int combined:
// ** TIME
const char* cch_timeMonthNames[] = {"Januar", "Februar", "März", "April", "Mai", "Juni", "Juli", "August", "September", "Oktober", "November", "Dezember"};
const char* cch_timeWeekdayNames[] = {"Sonntag", "Montag", "Dienstag", "Mittwoch", "Donnerstag", "Freitag", "Samstag"};
char* ch_timeMonthName;
char* ch_timeWeekdayName;
const char* cch_timeFullDate;
int int_timeMinute;
int int_timeHour;
int int_timeDay;
int int_timeMonth;
int int_timeWeekday;
void initTime(){
struct tm timeinfo;
Serial.println("Setting up time");
configTzTime("CET-1CEST,M3.5.0/02,M10.5.0/03", "ptbtime1.ptb.de");
if(!getLocalTime(&timeinfo)){
Serial.println(" Failed to obtain time");
return;
}
Serial.println(&timeinfo, "%A, %d %B %Y %H:%M:%S");
}
boolean getCurrentTime(){
struct tm timeinfo;
Serial.println("Get current time");
if(!getLocalTime(&timeinfo)){
Serial.println(" Failed to obtain time");
return false;
}
int_timeMinute = timeinfo.tm_min;
int_timeHour = timeinfo.tm_hour;
int_timeDay = timeinfo.tm_mday;
int_timeWeekday = timeinfo.tm_wday;
int_timeMonth = timeinfo.tm_mon;
ch_timeMonthName = cch_timeMonthNames[int_timeMonth];
ch_timeWeekdayName = cch_timeWeekdayNames[int_timeWeekday];
cch_timeFullDate = printf("%s, %d. %s", ch_timeWeekdayName, int_timeDay, ch_timeMonthName);
return true;
}
Any help is appreciated
Drop the "*" below and and perform a standard character array definition:
zimbo86:
const char* cch_timeMonthNames[]= {"Januar", "Februar", "März", "April", "Mai", "Juni", "Juli", "August", "September", "Oktober", "November", "Dezember"};
const char* cch_timeWeekdayNames[] = {"Sonntag", "Montag", "Dienstag", "Mittwoch", "Donnerstag", "Freitag", "Samstag"};
For example:
const char cch_timeMonthNames[][10]= {"Januar", "Februar", "März", "April", "Mai", "Juni", "Juli", "August", "September", "Oktober", "November", "Dezember"};
const char cch_timeWeekdayNames[][12] = {"Sonntag", "Montag", "Dienstag", "Mittwoch", "Donnerstag", "Freitag", "Samstag"};
Please post an error messages, using code tags and explain where you are having the difficulty.
To combine you can use sprintf
const char * dayOfWeek = "Monday";
const char * month = "December";
uint8_t day = 12;
char result[30];
void setup()
{
Serial.begin(115200);
sprintf(result, "The date is %s %d %s", dayOfWeek, day, month);
Serial.println(result);
}
void loop()
{}
10:51:21.910 -> The date is Monday 12 December
zimbo86
December 3, 2022, 10:27pm
4
i tried the following:
// ** TIME
const char cch_timeMonthNames[][10] = {"Januar", "Februar", "März", "April", "Mai", "Juni", "Juli", "August", "September", "Oktober", "November", "Dezember"};
const char cch_timeWeekdayNames[][12] = {"Sonntag", "Montag", "Dienstag", "Mittwoch", "Donnerstag", "Freitag", "Samstag"};
boolean getCurrentTime(){
struct tm timeinfo;
Serial.println("Get current time");
if(!getLocalTime(&timeinfo)){
Serial.println(" Failed to obtain time");
return false;
}
int_timeMinute = timeinfo.tm_min;
int_timeHour = timeinfo.tm_hour;
int_timeDay = timeinfo.tm_mday;
int_timeWeekday = timeinfo.tm_wday;
int_timeMonth = timeinfo.tm_mon;
ch_timeMonthName = cch_timeMonthNames[int_timeMonth];
ch_timeWeekdayName = cch_timeWeekdayNames[int_timeWeekday];
cch_timeFullDate = printf("%s, %d. %s", ch_timeWeekdayName, int_timeDay, ch_timeMonthName);
return true;
}
This leads to an error:
In function 'boolean getCurrentTime()':
xxxxx:58:54: error: invalid conversion from 'const char*' to 'char*' [-fpermissive]
ch_timeMonthName = cch_timeMonthNames[int_timeMonth];
You can't assign a value to a pointer like that.
There is no need to use any pointers in your application. Just use sprintf() to move the value into the printable ASCII C-string that you want.
Try this:
void setup() {
Serial.begin(115200);
while(!Serial);
const char cch_timeMonthNames[][10] = {"Januar", "Februar", "März", "April", "Mai", "Juni", "Juli", "August", "September", "Oktober", "November", "Dezember"};
char buf[40];
sprintf(buf, "Monat: %s", cch_timeMonthNames[1]);
Serial.println(buf);
}
void loop() {}
or use strcpy() to copy the string wherever you like.
zimbo86:
This leads to an error:
Just do as I showed above...
red_car:
char result[30];
red_car:
sprintf(result, "The date is %s %d %s", dayOfWeek, day, month);
system
Closed
June 2, 2023, 1:52am
7
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.