3 ms·
It’s an awkward holdover from bygone days. If you go back far enough in time, before it was really “C”, there was no difference between pointers and integers. T
by klodolph 7y ago
It’s an awkward holdover from bygone days. If you go back far enough in time, before it was really “C”, there was no difference between pointers and integers. The shift happened gradually, with pointers turning into a different type. I suspect NULL was kept as 0 to avoid breaking existing code. Then you can just:
#define NULL 0
This works in both C and C++. Conveniently, on most modern systems, a null pointer also points to memory address 0. So you can also do this:
struct mystruct {
int length;
int *data;
};
struct mystruct x;
memset(&x, 0, sizeof(x));
On modern systems this will initialize x.data to NULL. It just writes zero bytes into the structure, and since the NULL pointer is represented just by a bunch of zero bytes, you can conveniently zero-initialize structures with pointers this way (again, on modern systems). Strictly speaking, this is not portable, but it will only break on fairly esoteric systems.
- colejohnson66 7y agoFun reading on what systems didn’t use 0 for NULL: https://stackoverflow.com/questions/2597142/when-was-the-null-macro-not-0 https://stackoverflow.com/questions/2597142/when-was-the-nul...