17

C allows NULL to be defined to any null pointer constant, in other words, any integer constant expression that evaluates to 0, or such an expression cast to void *. My question concerns whether the choice of definition really matters, i.e. whether an otherwise-correct program might depend on which definition is used. For the purpose of this question, I'd like to ignore issues like NULL being passed to variadic functions or functions lacking prototypes, since I've already dealt with it separately. Let's assume sizeof NULL == sizeof(void *) and sizeof NULL == sizeof(T) for some integer type T, so that sizeof is not sufficient to answer the question of whether NULL has pointer type.

Obviously, C11 provides a way to distinguish the type of NULL or any other expression: the _Generic keyword.

C99 also provides one obscure way that seems to be reliable:

int null_has_ptr_type()
{
    char s[1][1+(int)NULL];
    int i = 0;
    return sizeof s[i++], i;
}

Are there any other methods by which the type of NULL may be determined by a conforming C program? Any that work in C89?

16
  • 1
    char c = NULL; generates compiler warnings if NULL is of type void * (so the cast is present). Jan 9, 2013 at 15:52
  • 1
    Does the function null_has_ptr_type really work? It returns 0 in both case on my computer.
    – md5
    Jan 9, 2013 at 15:53
  • return sizeof s[i++], i; will return i. Jan 9, 2013 at 15:57
  • 4
    You need a C compiler with C99 support including VLAs and proper implementation of sizeof in that case. If s is a VLA, i++ is evaluated, otherwise it's not. And s will be a VLA if NULL is a pointer, not if it's an int. Or something like that... GCC gives different warnings in both cases. @Kirilenko
    – Mat
    Jan 9, 2013 at 16:07
  • 2
    @R..: gcc.gnu.org/gcc-4.4/c99status.html states "broken" VLA support. 4.5 says it's ok. (4.6.1 works.)
    – Mat
    Jan 9, 2013 at 16:27

4 Answers 4

6

Through the question, answers, and comments, I think we established:

  1. The C11 way is easy (_Generic).
  2. The C99 way is fairly unreliable due to buggy compilers.
  3. Stringifying approaches are a dead-end due to typedef.
  4. No other approaches were found.

So the answer seems to be that there's no reliable pre-C11 method, and seemingly no valid pre-C99 method.

4

Get the string definition of NULL and then do an as complete check as you want. Here is a very simple minded one:

#define XSTR(x) #x
#define STR(x) XSTR(x)

if (STR(NULL)[0] == '(') {
   ...
}

But I don't know how you'll handle a __null which can come out from that.

6
  • @R.., there was a reason for which I proposed a very simple minded one ; Jan 9, 2013 at 16:13
  • Does this work for arbitrary levels of macro indirection? e.g. #define NULL _NULL1, #define _NULL1 _NULL2, #define _NULL2 _NULL3, ..., #define _NULL999 0? I think not.. Jan 9, 2013 at 16:14
  • 1
    @R..: It seems to have been established that the definition can be detected in theory. Is there a practical need for a robust solution? Jan 9, 2013 at 16:16
  • 2
    @Oli: Nope, actually I'm more interested in ways applications might inadvertently depend on the definition. Jan 9, 2013 at 16:17
  • 1
    @R.., as you excluded variadic functions (where the difference can lead to UB), I don't see a plausible case for inadvertent dependence. And I'd expect it to be addressed or at least folklore by now if there was one (especially that (void*)0 is common for C implementation and non conforming for C++). Jan 9, 2013 at 16:24
3

Couldn't you stringify the macro and look at the string?

# include <stddef.h>
# include <stdio.h>
# include <string.h>

# define STRINGIFY(x) STRINGIFY_AUX(x)
# define STRINGIFY_AUX(x) #x

int main(void)
{
  const char *NULL_MACRO = STRINGIFY(NULL);

  if (strstr("void", NULL_MACRO) != NULL)
    puts("pointer");
  else
    puts("integer");
}

It correctly prints "integer" if you add this (usually NULL has pinter type):

# undef NULL
# define NULL 0

NULL cannot be something like (int) ((void *) 0) because the standards doesn't state that a null pointer constant converted to an integer type is still a null pointer constant.

Furthermore, the standard also say this about integer constant expressions (C11, 6.6/6):

An integer constant expression117) shall have integer type and shall only have operands that are integer constants, enumeration constants, character constants, sizeof expressions whose results are integer constants, _Alignof expressions, and floating constants that are the immediate operands of casts. Cast operators in an integer constant expression shall only convert arithmetic types to integer types, except as part of an operand to the sizeof or _Alignof operator.

EDIT: actually this doesn't work with things like:

# define NULL (sizeof(void *) - sizeof(void *))

(thanks for noticing) and this cannot be checked in a trivial way as the OP needs, a little bit of work (simple parsing) is required.

EDIT 2: and there are also typedef as comment correctly pointed out.

28
  • 2
    @netcoder: That's the other way around. An integer constant expression with value 0 is still a null pointer constant when you cast it to void *. But casting a pointer to an integer type never yields an integer constant expression. Jan 9, 2013 at 16:18
  • 1
    Obviously, the plausible difficult case to handle is #define NULL __null where __null is an intrinsic of the compiler. Jan 9, 2013 at 16:33
  • 1
    @R.. my previous comment was wrong. Anyway, you're lucky, there are no standard integer types beginning with v :) I think your idea could work and it's quite easy.
    – effeffe
    Jan 9, 2013 at 17:20
  • 1
    @R.. Checking for 'v' need not work: typedef void *GenericPointer;. Jan 9, 2013 at 20:39
  • 2
    But a typedef is not just an equivalent type, it's another name for the same type, so casting to GenericPointer is casting to void*. Jan 9, 2013 at 21:15
1

Here's one obscure way: If the program uses the expression &*NULL, this won't compile with NULL having integer type but will if NULL has pointer type.

C99 has wording for this special case:

If the operand [of the & operator] is the result of a unary * operator, neither that operator nor the & operator is evaluated and the result is as if both were omitted, except that the constraints on the operators still apply and the result is not an lvalue.

The constraints on the operators aren't violated: the operand of & is the result of the unary * operator, and the operand of the unary * operator has pointer type (as we are assuming NULL is defined that way).

1
  • I think the question is looking for something that will compile whether NULL is of pointer or integer type. NULL+NULL compiles if and only if NULL is of integer type. Mar 10, 2016 at 5:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.