I am trying to prevent recreating the wheel. I am parsing an input file that has time as one of the values. I need a data structure to hold all of the values of the input file, and instead of creating a custom structure for the time field I want to just use struct tm from ctime.
I’m running into a strange error though, so hopefully one of you can help me out. Here’s my test code (for my proof of concept):
#include <ctime>
#include <cstdio>
int main()
{
int Oldhour = 16;
int OldSecond = 25;
int OldMinute = 20;
time_t seconds;
struct tm * timeinfo;
timeinfo->tm_hour = Oldhour;
timeinfo->tm_min = OldMinute;
timeinfo->tm_sec = OldSecond;
int hour, min, sec;
hour = timeinfo->tm_hour;
min = timeinfo->tm_min;
sec = timeinfo->tm_sec;
printf("%d:%d:%d", hour, min, sec);
return 0;
}
This compiles just fine and it does exactly what I want and prints “16:20:25” so it’s storing the info the way I want. However if I remove the line “time_t seconds;” it crashes immediately.
You need to allocate the structure either on the stack or on the heap with malloc. Specifically, you’re declaring a pointer to the struct without allocating any storage for it.
Try this: