C结构搞砸了 [英] C structure screw up

查看:107
本文介绍了C结构搞砸了的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我在使用以下代码时遇到麻烦.请引导我找到正确的资源,或指出我要去哪里了.

Hello,

I am having trouble with the following code. Please direct me to the right resource, or point out where am I going wrong.

typedef struct _file
{
    TCHAR        *name;
    struct _file *parent;
} file;

void SetName(file* image)
{
    (*image).name = \\..Some string.
}

void PrintName(file* image)
{
    _tprintf(_T("Name=%s\n"), (*image).name);
}

int _tmain(int argc, _TCHAR* argv[])
{
    file fileArray[10];
    SetName(&fileArray[0]);
    PrintName(&fileArray[0]);

    return 0;
}



我遇到的问题是,如果我在SetName函数中打印名称"的值,则可以正确打印,但是在PrintName函数中,它会被弄乱.

与在函数之间传递结构的方式有关吗?

非常感谢您的帮助.

谢谢.



因此,这里有详细的问题.

将项目设置设置为使用UNICODE字符集.




The problem i am having is that if i print the value of "name" in the SetName function, it prints correctly, but in the PrintName function, it gets screwed up.

Something to do with the way the structure is being passed between functions?

Any help is much appreciated.

Thanks.



So here goes the detailed problem.

The project settings are set to use UNICODE character set.


typedef struct _file
{
    wchar_t      *name;
    struct _file *parent;
} file;
void SetName(_TCHAR* dir, file* image)
{
    TCHAR filePath[MAX_PATH];
    StringCchCopy(filePath, MAX_PATH, dir);
    (*image).name = filePath;
}
void PrintName(file* image)
{
    _tprintf(_T("Name=%s\n"), (*image).name);
}
int _tmain(int argc, _TCHAR* argv[])
{
    file fileArray[10];
    SetName(argv[1], &fileArray[0]);
    PrintName(&fileArray[0]);
    return 0;
}



此代码会导致问题.

发表评论后,我尝试了其他一些可行的方法-
1.为_T("blahblah")分配名称有效.
2.将每个字符从filePath复制到名称都可以.

请解释一下这种感觉.以及解决问题的方法.

非常感谢所有评论.



This code causes the problem.

After the comments, I tried a few other things which worked -
1. Assigning name to _T("blahblah") works.
2. Copying each character from filePath to name works.

Please explain the sense out of this. And also the solution to the problem.

Thanks a lot for all the comments.

推荐答案

_ abhi_写道:
_abhi_ wrote:

(*图片).name = \\ ..一些字符串.

(*image).name = \\..Some string.

这是关键行(您没有向我们显示实际代码).它仅适用于string literals<code>static(或全局)字符数组,在其他情况下,您必须为字符串分配内存.

This is the critical line (and you didn''t show us the actual code). It would work just with string literals and <code>static (or global) character arrays, in other cases you have to allocate memory for the string.


在VC2008(在WinXP SP3上)上尝试过

二手货:

Just tried it in VC2008 (on WinXP SP3)

Used:

(*image).name = _T("cccvbncxvbncvbncvbncvbncvbn");



对我来说很好.

带有:



Worked fine for me.

With:

(*image).name = (TCHAR *) "cccvbncxvbncvbncvbncvbncvbn";



我发垃圾了.
毫无疑问,我可以尝试您\\ .. Some字符串的其他变体.
你在用什么?

仅供参考,我已经在适用于各种情况的C ++测试应用程序中做到了这一点-您在C ++中而不是在普通C语言中尝试过吗?



I get junk.
No doubt I could try other variations of your \\..Some string.
What are you using?

FYI I''ve done this in a C++ test app that I adapt for all sorts of things - have you tried it in C++ rather than plain C?


这解决了问题-

This solves the problem -

struct
{
    wchar_t name[MAX_PATH];
}







and

wcsncpy_s((*image).name, filePath, MAX_PATH);


这篇关于C结构搞砸了的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆