2016-11-11 68 views
1

我的代碼中使用了time_t和Struct tm。我無法像我正在做的那樣初始化結構體。如果這是在函數中初始化,那麼它工作正常。請幫助編譯時的time_t聲明問題

#include "time.h" 
static struct tm strtime; 
struct tm * timeinfo; 
time_t startTime; 
time_t endTime; 
time_t curTime; 
time_t candleStartTime; 
strtime.tm_hour = 9; //Error here 
strtime.tm_min = 15; //Error here 
strtime.tm_sec = 00; //Error here 
void PrintMarketData(void **args,nsString sPfName) 
{ 
    curTime = time (NULL); 
    timeinfo = localtime (&curTime); 
    int curYear = timeinfo->tm_year; 
    int curMonth = timeinfo->tm_mon; 
    int curDay = timeinfo->tm_mday; 

    strtime.tm_year = curYear; 
    strtime.tm_mon = curMonth; 
    strtime.tm_mday = curDay; 
} 

回答

1

對於這三個行:

strtime.tm_hour = 9; //Error here 
strtime.tm_min = 15; //Error here 
strtime.tm_sec = 00; //Error here 

不能初始化的全局實例一樣,在全球範圍內(由線賦值語句行)。這已是一個函數中完成:

你可以試試這個:

struct tm strtime = {0, 15, 9}; 

可能的工作假設strtime的成員tm_sec,tm_min的預期的順序聲明,其次是tm_hour。但我無法保證每個平臺上的struct tm的會員訂單是否標準。

說實話,只是在main的早期階段進行顯式初始化更好。

+0

'struct'成員訂單總是標準化的。 – Potatoswatter