开发者

wchar_t array passed into function

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开发者_如何学C, 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.


in main you are calculating the size of the array:

sizeof(bla); // 1024 * sizeof(unsigned short) = 2048 

in your function, if you use sizeof, you are calculating the size in bytes of a pointer:

sizeof(bla); //means sizeof (wchar_t *) = 4


I find it odd that the argument function is of type wchar_t in VS2010, its been a long time since wchar_t was just a typedef for unsigned 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 within mymethod so if you need it, add it as another parameter.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜