C语言中,对 sizeof() 的处理都是在编译阶段进行。

下面代码,注意可变参数是怎么使用的

#include<Windows.h>
#include<stdio.h>

int MessageBoxPrint(char *szFormat, ...);
int fun(char ch[]);

int WinMain(HINSTANCE hInst, HINSTANCE tmp, LPSTR strCmd, int nShow)
{
    char str[1024];
    MessageBoxPrint("%p", hInst);
    MessageBoxPrint("ch=%d", fun(str));
    return 0;
}

int MessageBoxPrint(char *szFormat, ...)
{
    char buf[1024];
    va_list va;
    va_start(va, szFormat);
    vsnprintf(buf, sizeof(buf), szFormat, va);
    va_end(va);
    return MessageBox(NULL,buf,"printf",MB_OK);
}

int fun(char ch[])
{
    return sizeof(ch);
}
View Code

相关文章: