How to set the pointer to a zero location?

As I know, all NULL occurrences in the code are replaced with 0 at the preprocessing stage. Then, at compile time, all occurrences of 0 in the context of the pointer are replaced with the corresponding value, which is NULL on this machine. Thus, the compiler should know that the value is NULL for this particular machine.
Now this means that whenever I use 0 in the context of the pointer, it is replaced with a corresponding value representing NULL on this computer, which may or may not be 0. So, how can I tell the compiler what I really mean 0 and not NULL when I use 0 in a pointer context?
Sorry for the long description. Correct me if I am wrong

+3
source share
2 answers

One way is to save all bit zero in your pointer:

void* zero;
memset(&zero, 0, sizeof(zero));
+4
source

Well, there is no portable way to achieve this, in C. C does not provide any portable functions specifically designed to point to a specific numerical address. However, the "secret" intention of explicitly converting an integer pointer is actually this: implement a "natural" comparison between integers and pointers, where "natural" usually means that the numeric value of the integer remains unchanged (if possible) when converting it to a pointer type . In other words, all you need is an integer with value0. , , . ( , , ).

,

uintptr_t i = 0;
void *p = (void *) i;

, 0. , C ( ++)

const uintptr_t i = 0;
void *p = (void *) i;

, 0.

void *p = 0; /* compile-time 0 will not do */

.

C FAQ , .

+3

Source: https://habr.com/ru/post/1736224/


All Articles