Why I get NULL is undefined error?
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.
NUL undeclared- first use in this function
There's NULL and then there's NUL.
NULL is defined in stddef.h, is used very widely, and is a reference to a null pointer.
NUL is different - it is the first character in the standard ASCII character set, and more importantly, it is not a standard macro. You may have to define it yourself.
To define NUL, do:
#define NUL '\0'
set head to NULL ('NULL' : undeclared identifier)
As written, NULL
isn't defined in your program. Usually, that's defined in a standard header file -- specifically <cstddef>
or <stddef.h>
. Since you're restricted to iostream
, if yours doesn't get NULL
implicitly from that header, you can use 0
or, in C++11, nullptr
, which is a keyword and doesn't require a header. (It is not recommended to define NULL
yourself. It might work sometimes, but it is technically illegal.)
null undeclared identifier Error
Replace null
with NULL
. NULL
is a implementation-defined macro that represents the null pointer constant (18.1.4 in the standard). Usually, it's:
#define NULL 0
NULL undeclared identifier in C++/CLI
you have to include stdlin.h or stddef.h, I believe
What is the difference between null and undefined in JavaScript?
undefined
means a variable has been declared but has not yet been assigned a value :
var testVar;
alert(testVar); //shows undefined
alert(typeof testVar); //shows undefined
Related Topics
Cmake Unable to Determine Linker Language with C++
Significance of Parentheses in Decltype((C))
Why Does the Most Negative Int Value Cause an Error About Ambiguous Function Overloads
Repeated Typedefs - Invalid in C But Valid in C++
Inheritance and Templates in C++ - Why Are Inherited Members Invisible
Calling a Const Function Rather Than Its Non-Const Version
How to Signify No More Input for String Ss in the Loop While (Cin >> Ss)
Rand() Generating the Same Number - Even with Srand(Time(Null)) in My Main!
Comparing Floating Point Number to Zero
How to Reverse the Order of Arguments of a Variadic Template Function
How to Write C++ Code Without Headers (Repetitive Function Declarations)
Template Specialization and Enable_If Problems
Will C++ Exceptions Safely Propagate Through C Code
How Do We Explain the Result of the Expression (++X)+(++X)+(++X)