-1

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?

alk
  • 69,737
  • 10
  • 105
  • 255
bsky
  • 19,326
  • 49
  • 155
  • 270

1 Answers1

1

"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)
Spock77
  • 3,256
  • 2
  • 30
  • 39