2016-07-10 172 views
-1

以下函數在控制檯上打印垃圾。無法理解問題。以下是測試代碼片段。變量參數列表打印垃圾

void 
format(
    const char* pcszFormat, 
    ... 
    ) 
{ 
    va_list VarArglist; 

    va_start(VarArglist, pcszFormat); 
    printf(pcszFormat, VarArglist); 
    va_end(VarArglist); 
} 


int wmain(int argc, wchar_t *argv[]) 
{ 
    string strTest; 
    const char *pcszFormatted; 

    format("%x %S", 10, "Test"); 

    //printf("\nFormatted string: %s", pcszFormatted); 
    getchar(); 
    return 0; 
} 
+0

請縮進你的代碼! – 2501

回答

1

您通過將錯誤類型的數據傳遞到printf()來調用未定義的行爲。您應該使用​​與va_list一起使用。

也不要忘記,以便與普通字符串工作或爲了與寬字符串工作變動​​到L"Test",或者你會被傳遞有錯誤類型printf()數據調用未定義行爲改變%S%s家人再次。

0

你不能傳遞到va_listprintf。改爲使用vprintf