I have to return nul in a function but I'm not allowed to include any library. I tried to find in how is NULL defined, then to sys/_types/_null.h only to find that NULL is actually __DARWIN_NULL. Great ! Now, I have no idea where to search in order to find the __DARWIN_NULL definition...
-
2Possible duplicate of http://stackoverflow.com/questions/18781514/correct-way-of-defining-null-and-null-pointer – Ari0nhh Jul 23 '15 at 18:45
-
1AFAIK, `__DARWIN_NULL` is a compiler built-in, so you won't find it in any header file. – user3386109 Jul 23 '15 at 18:50
-
4Unclear whether you want the `nul` char or the `NULL` pointer. Whichever you can `return 0`. – Weather Vane Jul 23 '15 at 19:19
1 Answers
I have to return nul in a function but I'm not allowed to include any library.
The solution to this problem is far simpler than you're making it; you've actually asked a form of XY problem.
You won't find the NUL character defined in any standard library; the best way to return that will be using the constant '\0'
or 0
.
If your professor is teaching you to avoid using <stddef.h>
to find NULL
then he/she has set a silly exercise which involves using something other than the most appropriate tool for the job, a tool which is guaranteed by the standard to exist, by the way... I would be raising this as a concern.
Nonetheless, sometimes professors don't care and will teach you to do stupid things anyway. NULL
is defined as an implementation-defined null pointer constant, usually 0
or a conversion of 0
to void *
like so: ((void *) 0)
. That may not be the implementation-defined value your NULL
resolves to; in that case, adjust to suit :)
You could add a preprocessor definition such as #define NULL ((void *) 0)
and then you would be able to return NULL;
from your functions. Ta-da! Stupid exercises deserve stupid solutions.
If your professor says you're not allowed to use #define
, either, I would be tempted to ask him what you are allowed to use. Changing requirements on the fly is not fair. Most compilers will allow you to set preprocessor constants using a command line argument, for example cc -DNULL='((void *) 0)' ...
. This is useful for exposing compile-time configuration options, but again, using this to define NULL
is dumb.
The question in your title is different to the rest of your post. __DARWIN_NULL
could also be defined using either of the above, providing it isn't already defined, but I really don't think that's required to answer your actual question.

- 1
- 3
- 35
- 80