문제

I'm testing to see that the NULL constant does indeed occupy the size of a pointer:

ASSERT(sizeof NULL == sizeof(char*));

However, I accidentally wrote the following instead:

ASSERT(sizeof NULL == sizeof char);

That should have compiled, but instead it gave me the following error:

error: expected expression before ‘char’

The same happened after I enclosed NULL in brackets

ASSERT(sizeof(NULL) == sizeof char);

Isn't the NULL constant usually suppose to be defined by a macro which associates it to a pointer which is equal to 0? The statement was obviously false but as far as I see there was no syntactic error. If that is true, why was I receiving a compilation error?

도움이 되었습니까?

해결책

"When the operand is a type name, it must be enclosed in parentheses": C sizeof operator

In C, NULL is usually defined as

#define NULL ((void*)0)
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top