On enSO they wrote that NULL is (void *)0 .
But I don’t understand how the constant NULL is represented by a pointer to a void type?
Well, the code:
#include "stdio.h" int main(void) { printf("%d\n",sizeof(NULL)); /// 8 return 0; } On enSO they wrote that NULL is (void *)0 .
But I don’t understand how the constant NULL is represented by a pointer to a void type?
Well, the code:
#include "stdio.h" int main(void) { printf("%d\n",sizeof(NULL)); /// 8 return 0; } The sizeof operator has the size_t return type. Therefore, it is correct to specify the format specifier zu
printf("%zu\n",sizeof(NULL)); ^^^^^ Otherwise, the printf function generally has an undefined behavior.
As for your question, according to the C language standard (6.3.2.3 Pointers)
If you want to see a pointer, it’s not necessary. it can be compared to any object or function.
and (7.19 Common de fi nitions)
3 The macros are
NULLwhich expands to an implementation-de-fined null pointer constant ;
That is, the answer to your question follows from the definition of the constant null pointer constant and the definition of the macro NULL .
On your system, pointers include null pointer constants , which is usually defined in C as
( void * )0 have a size of 8 bytes.
sizeof NULL implementation defined and can be either the size of a pointer, or the size of an int? - Qwertiy ♦#define NULL (void*)0 it is forbidden to simply #define NULL 0 , as it is done in pluses. It turns out that sizeof NULL is equal to either sizeof (void*) or sizeof (int) (in the variant sizeof 0 ). What exactly is the error of such reasoning? Or is there still a significant piece of the standard that you did not include in the answer? - Qwertiy ♦void* . - Qwertiy ♦First, in C, NULL is either (void *) 0 or an integer constant 0 (just 0 , 0LL , 0LL , etc.) So there is no predetermined specific size for the result of a NULL expression in 0LL
Secondly, it is pointless to discuss the output of your code because of the attempts to print the sizeof result in %d . The result of sizeof is of type size_t , which is generally not compatible with the %d format specifier.
However, in any case, there would be nothing unusual if on your platform NULL were declared as (void *) 0 and the size of the result of such an expression would be 8 bytes. Therefore, it is not entirely clear what caused your question.
%zu for printing values of type size_t . - AnTAt least in Visual C ++
printf("%d\n",sizeof(NULL)); the same as
printf("%d\n",sizeof(void*)); Accordingly, the displayed value depends on which code is compiled and what :)
So, Visual C ++ will give 4 when compiling a 32-bit application, and 8 when compiling a 64-bit application. Once again - the application , not the operating system .
Update
From Windows Kits -
#ifndef NULL #ifdef __cplusplus #define NULL 0 #else #define NULL ((void *)0) #endif #endif So in C ++ mode in both versions, Visual C ++ will give out 4. But, since the tag was " c ", that's right ...
NULL is similarly described in Open Watcom.
Quote from "From the Handbook. Full description of the language":
A null pointer constant is an integer constant with a value of 0 , or a constant integer value of 0 , converted to a pointer type of void . NULL defined in stdlib.h , stdio.h and other header files.
Update 2
From the standard -
The macro NULL is defined in (and other headers) as a null pointer constant; see 7.17.
The macros are null pointer constant.
Source: https://ru.stackoverflow.com/questions/612766/
All Articles