6

So, I had an argument with my professor earlier defending that NULL is not a pointer, but he kept on insisting that it is because there is such a thing as NULL pointer. So, here I am now a little bit confused if NULL is really a pointer or not

I already tried search over the internet but couldn't find any answer, so my last resort is here

6
  • So what do you think when assigning NULL to a pointer? What does it contain? Commented Apr 4, 2019 at 16:48
  • 1
    It depends on what you mean by "is a pointer". Since you tagged this "conceptual", I think you may be using the term "pointer" in a sense different from the formal language definition. Commented Apr 4, 2019 at 16:50
  • @phuclv the pointer points to nothing or zero Commented Apr 4, 2019 at 16:54
  • a pointer that points to nothing is a null pointer Commented Apr 5, 2019 at 1:31
  • @Joe: you can accept one of the answers by clicking on the grey checkmark below its score. Commented Apr 13, 2019 at 11:35

3 Answers 3

4

In C, NULL is a macro that expands to a null pointer constant.

7.19p3

The macros are

NULL which expands to an implementation-defined null pointer constant; ...

A null pointer constant is an integer constant expression with the value 0 ( e.g., 0, 1-1, 42*0LL, etc.) or such an expression cast to (void*).

6.3.2.3p3

An integer constant expression with the value 0, or such an expression cast to type void *, is called a null pointer constant.66) If a null pointer constant is converted to a pointer type, the resulting pointer, called a null pointer, is guaranteed to compare unequal to a pointer to any object or function.

Most common C implementations define NULL to be 0, 0L, or ((void*)0).

So you are correct. NULL need not be a pointer.

(IIRC, C++ doesn't even allow the (void*) cast in NULL, meaning NULL in C++ always has integer type. Because of that and because void* pointers do not compare with regular pointers so readily in C++, C++>=11 now has a special nullptr keyword.)

Sign up to request clarification or add additional context in comments.

3 Comments

@Joe If it has the (void *) cast in it, then it does have (the void) pointer type.
@Joe NULL is a macro expanding to a nonspecific value which can be compared with pointers for equality. The NULL value may have the void-pointer type or it may be an integer. Usually it's 0, 0L or ((void*)0).
@PSkocik Note that this question was tagged "conceptual", not "language-lawyer", so my guess is that the question is not so much about NULL the macro as it is about "the null pointer" as a concept.
3

NULL itself is not a pointer, it is a macro that can be used to initialize a pointer to the null pointer value of its type. When compared to a pointer, it compares equal if the pointer is a null pointer and unequal if the pointer is a valid pointer to an object of its type.

There is no semantic difference between char *p = 0; and char *p = NULL; but the latter is more explicit and using NULL instead of 0 is more informative in circumstances where the other operand is not obviously a pointer or if comparing to an integer looks like a type mismatch:

FILE *fp = fopen("myfile", "r"); if (fp == NULL) { /* report the error */ } 

Similarly, there is no semantical difference in C between '\0' and 0, they both are int constants. The first is the null byte, the second the null value. Using 0, '\0' and NULL wisely may seem futile but makes code more readable by other programmers and oneself too.

The confusion may come from misspelling or mishearing the null pointer as the NULL pointer. The C Standard was carefully proof read to only use null pointer and refer to NULL only as the macro NULL.

Note however that one the accepted definitions of NULL, #define NULL ((void*)0) makes NULL a null pointer to void.

Comments

0

There's nothing about the idea of a 'pointer to address 0' that's a problem.

The rule is that you're disallowed from derefencing it... it's allowed to exist, and if created will meet any criteria for "pointerhood" I can think of. Just because it's not meaningfully a pointer to something...

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.