Search code examples
carraysvisual-studio-2010sizeofwchar-t

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, 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.


Solution

  • 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