3 ms·
I'm not a C programmer, rather an extensive database user. And NULL == 0 seems awkward and nonsensical to me.
by oarabbus_ 6y ago
I'm not a C programmer, rather an extensive database user. And NULL == 0 seems awkward and nonsensical to me.
- klodolph 6y agoIt’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 6y 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...
- ken 6y agoLooking at popular SQL databases (you know who you are!), NULL == '' seems pretty awkward to me, too!