wchar_t 数组传递给函数
我正在使用 Visual Studio 2010 在 Windows 上制作 C 程序。
我将 wchar_t 数组传递给函数。
//in main
wchar_t bla[1024] = L"COM6";
mymethod(bla);
static void mymethod(wchar_t *bla) {
//do stuff
}
我使用调试器观察 bla,sizeof(bla) 并注意到在 main 中,bla 的类型为 wchar_t
且 sizeof(bla) = 2048
但在 mymethod 中,bla 是类型为 unsigned Short*
且具有 sizeof(bla) = 4
。
为什么会这样呢?
我想将 bla 传递到该方法中,以便该方法可以更改数组而不是返回编辑后的数组。但是,swprintf 不起作用,因为我希望 sizeof(bla) 为 1024 而不是 4。
干杯。
I am making a C program on windows using visual studio 2010.
I am passing a wchar_t array to a function.
//in main
wchar_t bla[1024] = L"COM6";
mymethod(bla);
static void mymethod(wchar_t *bla) {
//do stuff
}
I used the debugger to watch bla, sizeof(bla) and noticed that in main, bla is of type wchar_t
and sizeof(bla) = 2048
but in mymethod, bla is of type unsigned short*
and has sizeof(bla) = 4
.
Why is this the case?
I wanted to pass bla into the method so that the method could change the array instead of returning an edited array. However, swprintf is not working as I want sizeof(bla) to be 1024 instead of 4.
Cheers.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 main 中,您正在计算数组的大小:
在您的函数中,如果您使用 sizeof,您正在计算指针的大小(以字节为单位):
in main you are calculating the size of the array:
in your function, if you use sizeof, you are calculating the size in bytes of a pointer:
我觉得奇怪的是,VS2010中参数函数的类型是
wchar_t
,自从wchar_t
只是unsigned short
的typedef以来已经很长时间了。大小差异很明显,因为一个是数组,另一个是指针。如果这是 C++,你可以引用数组。您将无法从mymethod
中获取数组的大小,因此如果您需要它,请将其添加为另一个参数。I find it odd that the argument function is of type
wchar_t
in VS2010, its been a long time sincewchar_t
was just a typedef forunsigned short
. The difference size is obvious since one is an array and the other is a pointer. If this were C++ you could take a reference to the array. You won't be able to get the size of the array from withinmymethod
so if you need it, add it as another parameter.