从函数返回指向wchar_t的指针

时间:2012-10-15 11:35:58

标签: c++

为什么这段代码不起作用?

wchar_t path[300];
wchar_t *PathCreator(wchar_t *dir, wchar_t *fileName)
{
    int j = 0;
    while(j < 300)
    {
        path[j] = '\0';
        j++;
    }
    int i;
    i = 0;
    while(*dir != '*')
    {
        path[i] = *dir;
        i++;
        dir++;
    }
    wchar_t *t = fileName;  
    while(*t != '\0')
    {
        path[i] = *t;
        i++;
        t++;
    }
    path[i] = '\0';
    //std::wcout << path << std::endl;
    return path;
}
int _tmain(int argc, _TCHAR* argv[])
{ 
    std::wcout << PathCreator(L"A",L"B");
}

1 个答案:

答案 0 :(得分:1)

您的循环错误,它不会检查dir的结尾。所以

while(*dir != '*' && *dir != '\0')
{
    path[i] = *dir;
    i++;
    dir++;
}

while(*dir != '*')
{
    path[i] = *dir;
    i++;
    dir++;
}