EDIT: while someone thought this question is the same as the one they posted a link to. It is a very different question.
consider the following:
#define HUNDRED 100
typedef WCHAR BUFFER_SPACE[HUNDRED];
the type BUFFER_SPACE is an array with a size of 200 bytes (assume UTF16)
In most cases, declaring a variable such as:
BUFFER_SPACE abuffer = { 0 };
wprintf(L"sizeof abuffer : %zd\n", sizeof(abuffer));
will result in wprintf outputting 200 (as expected.)
Now, consider the following simple sample program:
#include "stdafx.h"
#include <Windows.h>
#include <WinNt.h>
#define HUNDRED 100
typedef WCHAR BUFFER_SPACE[HUNDRED];
void Function(BUFFER_SPACE abuffer)
{
BUFFER_SPACE anotherbuffer = { 0 };
wprintf(L"sizeof abuffer : %zd\n", sizeof(abuffer)); // 8
wprintf(L"sizeof *abuffer : %zd\n", sizeof(*abuffer)); // 2
wprintf(L"sizeof anotherbuffer : %zd\n", sizeof(anotherbuffer)); // 200
// a highly undesirable solution is to apply sizeof to the type but, if the
// type of the parameter changes, the programmer has to be aware that the
// original/old type is being used in sizeof() someplace else in the
// function/program
wprintf(L"sizeof BUFFER_SPACE : %zd\n", sizeof(BUFFER_SPACE)); // 200
getchar();
}
int main()
{
BUFFER_SPACE abuffer = { 0 };
WCHAR anotherbuffer[HUNDRED] = { 0 };
wprintf(L"sizeof abuffer : %zd\n", sizeof(abuffer));
wprintf(L"sizeof anotherbuffer : %zd\n", sizeof(anotherbuffer));
wprintf(L"\n");
Function(abuffer);
return 0;
}
I wanted to get the entire size of the buffer parameter in the function. Using VC++ and compiling for 64 bit,
sizeof(abuffer) is 8 which makes sense since it is a pointer to the array
sizeof(*abuffer) is 2 which I consider rather questionable but, it is not something I want to debate.
sizeof(BUFFER_SPACE) is 200 as it should be.
My question is: Is there a way to get the value of 200 from the parameter (abuffer in this case) or is using the type the only way to get it ?