How to fix C error 'NULL undeclared'
Problem:
You have C code like
main.c
char* ptr = NULL;
but when you try to compile it you see an error message like
error_message.txt
main.c: In function ‘main’:
main.c:3:17: error: ‘NULL’ undeclared (first use in this function)
void* ptr = NULL;
^~~~
main.c:3:17: note: each undeclared identifier is reported only once for each function it appears in
Solution
Add
fix_null_error.c
#include <stddef.h>
at the top of the source file where the error occured.
The reason for this error is that NULL
is not defined in C itself but only in stddef.h
.
Check out similar posts by category:
C/C++, GCC Errors
If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow