【发布时间】:2014-09-26 19:50:42
【问题描述】:
运行此代码时:
TCHAR *getSettingsFilePath(TCHAR *defaultProfilePath)
{
TCHAR *prefPath;
PathCombine(prefPath, defaultProfilePath, "profile.xml");
return prefPath; // This returns valid path
}
编译器抛出警告:warning C4700: uninitialized local variable prefPath used
PathCombine 函数将prefPath 设置为所需的路径。
但是当我尝试在运行PathCombine 之前将prefPath 初始化为NULL 时,编译器警告消失了,但我的函数也返回了NULL。
TCHAR *getSettingsFilePath(TCHAR *defaultProfilePath)
{
TCHAR *prefPath = NULL;
PathCombine(prefPath, defaultProfilePath, "profile.xml");
return prefPath; // This will return NULL
}
我在这里想念什么?初始化这个指针的正确方法是什么?
【问题讨论】:
-
From the documentation: "您必须将此缓冲区的大小设置为 MAX_PATH 以确保它足够大以容纳返回的字符串。"并且由于您要返回它,因此最好是动态的或由调用者传入(通常首选后者)。
-
TCHAR *prefPath = (TCHAR*)calloc(MAX_PATH, sizeof(TCHAR));
标签: c windows winapi visual-c++