Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why I get NULL is undefined error?

Tags:

c

I have this thiis row:

while (**arr != NULL)

In this row I get this error:

identifier "NULL" is undefined

I included this library:

#include<stdlib.h>

but still I get the error above.Any idea how to fix this error?

like image 693
Michael Avatar asked May 16 '18 20:05

Michael


People also ask

Why is NULL undefined in C++?

This happens because NULL is not a built-in constant in the C or C++ languages. In fact, in C++, it's more or less obsolete, instead, just a plain 0 is used.

Where is NULL declared?

NULL is defined in the following header files: CRTDBG. H, LOCALE. H, STDDEF. H, STDIO.

What is undeclared error in C?

In C and C++ all names have to be declared before they are used. If you try to use the name of a variable or a function that hasn't been declared you will get an "undeclared identifier" error.


1 Answers

Both stdio.h and stdlib.h are, in fact, required to define NULL, all the way back to the original ANSI C standard in 19891 (unfortunately this is a .txt file, so I can't link to a specific section; search for 4.9 INPUT/OUTPUT <stdio.h> and/or 4.10 GENERAL UTILITIES <stdlib.h>, and then scroll down a little). If either of the minimized test programs

#include <stdio.h>
void *p = NULL;

or

#include <stdlib.h>
void *p = NULL;

fails to compile to an object file, then your C implementation is buggy. (If the above test programs do not fail to compile, you're gonna need to do some delta-minimization on your actual program, and probably then track down your wiseacre cow-orker who thought it would be funny to put #undef NULL in an application header file.)

NULL is also required to be defined in several other standard headers, but its true home, as you may guess from the cross-references to section 4.1.5 to explain what NULL is supposed to be defined to, is stddef.h. A C implementation that fails to define NULL in stddef.h is egregiously buggy. Also, stddef.h is one of the very few headers that is required to be provided by a "freestanding implementation"; if you are working in an embedded environment, it's possible that they thought they could get away with leaving NULL out of stdio.h or stdlib.h, but they have no excuse whatsoever for leaving it out of stddef.h.

In the alternative, just use 0 for the null pointer constant. That's perfectly fine style as long as all your functions have prototypes. (You have to cast it to pass it correctly to a function that takes a variable number of arguments, e.g. to execl, but you have to cast NULL to pass it correctly to a function that takes a variable number of arguments, so it comes out in the wash.)


1 Footnote for historians: yes, the linked document really is the ANSI C standard, not the ISO standard with nigh-identical wording (but very different section numbering) that came out a year later. I am not aware of any copy of the 1990 edition of the ISO C standard that is available online at no charge.

like image 121
zwol Avatar answered Sep 19 '22 13:09

zwol