2

If these 3 lines are the only time NULL is mentioned in stdio.h, how is NULL different from 0 and most importantly what tells the compiler NULL is actually an invalid address? Talking about C, maybe in other libraries, like in iostream, NULL is defined differently.

0085 #ifndef NULL
0086 #define NULL 0
0087 #endif

2 Answers2

2

To the compiler, NULL is indistinguishable from 0 when used as a pointer. The standard actually defines 0 to be a null pointer value, and the standard library just introduces a convenient macro NULL defined to a null pointer value (usually 0 or ((void*)0)), so that you can use it in code for better readability and expressing intent. But there's nothing special about NULL itself; it's the 0 that is relevant.

Angew is no longer proud of SO
  • 167,307
  • 17
  • 350
  • 455
1

To the compiler, all of 0, NULL and '\0' map to the same value. However, from the readability perspective, you may want to use the appropriate value depending on the context, to make it clear what you're trying to do and to improve the efficacy of code auditing and reviews.

cnst
  • 25,870
  • 6
  • 90
  • 122