Ceeker Ceeker - 2 years ago 425
C Question

nullptr undeclared(first use in this function)

I just wanted to practice with

instead of
in my code. I know that they are almost the same in many cases but I just wanted to practice (I find the difference out in What are the advantages of using nullptr?).

I've read Compile error 'nullptr' undeclared identifier and I downloaded the latest version of GNU GCC compiler and it worked correctly. And there was What header file needs to be included for using nullptr in g++? too. There is ZoogieZork's answer but it was for C++ not C. What is the reason for error?

Here a piece of code

FILE *fptr;


||=== Build: Debug in 1 (compiler: GNU GCC Compiler) ===|
E:\COOP\1\main.c||In function 'main':|
E:\COOP\1\main.c|12|error: 'nullptr' undeclared (first use in this function)|
E:\COOP\1\main.c|12|note: each undeclared identifier is reported only once for each function it appears in|
E:\COOP\1\main.c|27|error: expected declaration specifiers or '...' before '*' token|
||=== Build failed: 2 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|

Answer Source

nullptr is C++ only; it is not needed in C because in C ((void*)0) is convertible to any other pointer type without casts.

If you really really really like to type nullptr in C, you can use

#define nullptr ((void*)0)

and it would then work mostly the same.

Notice that C has the NULL macro from <stddef.h>; it is readable too, but its expansion is implementation-defined, so it might be either ((void*)0) or 0 (or something really strange); if it expands to 0, you wouldn't get any diagnostics from

int a = NULL;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download