In K&R2, the declarations and initializations of everything are separated. Also, when NULL is introduced, not once is it used in an initialization, but always as a return or comparison value.
Did the practice emerge subsequently, or did the writers think their way was adequate?
CodePudding user response:
In K&R2, the declarations and initializations of everything are separated.
Not the case. In the section on Declarations (section 2.4, page 40), you will read:
A variable may also be initialized in its declaration. If the name is followed by an equals sign and an expression, the expression serves as an initializer...
Various examples follow.
There is nothing special about NULL
; it's just a value which may be assigned to a pointer variable.
Many people believe that it is a good idea to always provide an initialiser, because if you don't, you run the risk of using an uninitialised variable. On the other hand, that practice does not really fix bugs. Erroneously using an arbitrary initial value might prevent undefined behaviour, but it is still an erroneous value, and you would arguably have been better off seeing the compiler warning about possible use of an uninitialised variable. (Always enable compiler warnings.)
At any rate, if you feel the need to initialise a pointer variable, NULL
is a convenient option. Sometimes it is even the correct option.
By the way, you'll find an example of initialising to NULL
at the top of page 187:
static Header base;
static Header *freep = NULL; /* start of free list */