5

Consider I have a structure

typedef struct point_t
{
  int x;
  int y;
}POINT;

I create a pointer to pointer for POINT and initialize it to NULL.

POINT **ppPoint = NULL;

Should *ppPoint also return NULL ?

Vikas Rokade
  • 112
  • 5
  • 15
    You can't dereference a null pointer, therefore `*ppPoint` is invalid. – Some programmer dude Sep 20 '19 at 06:56
  • 1
    A pointer to a pointer follows the same rules as all other pointers. – molbdnilo Sep 20 '19 at 06:58
  • 1
    @Bathsheba, can you state the reason to reopen this post? I closed as a duplicate of [What EXACTLY is meant by “de-referencing a NULL pointer”?](https://stackoverflow.com/questions/4007268/what-exactly-is-meant-by-de-referencing-a-null-pointer) -- because -- A pointer whether it's double pointer, triple pointer or quad pointer -- is a pointer. The rules apply on them in the same way. Would you reopen the questions, if someone asks the same as above for *N-th* pointer? – iammilind Sep 20 '19 at 07:27
  • @iammilind: It's a linked question, not a duplicate. – Bathsheba Sep 20 '19 at 07:28
  • Well, if one understands that a pointer-to-pointer isn't a special case, then it is kind of a dupe question. But that doesn't seem to be the case here. – Lundin Sep 20 '19 at 07:43

4 Answers4

11

The fact that there are 2 layers of pointers here is actually irrelevant. The fact that your outer pointer in NULL means it is illegal to dereference it. Therefore it is not meaningful to ask what value you would get by dereferencing it.

Consider this:

int *p = NULL; // modern practice would be to use nullptr anyway

if (0 == *p) // this is Undefined Behaviour!
{
    // do something, maybe?
}

You are just adding another layer to this, where instead of int, you have point_t*, but it really makes no difference.

The thing with Undefined Behaviour is that anything can happen! Your program might crash, or it might appear to work, or it might give you garbage values sometimes, or it might ...

BoBTFish
  • 19,167
  • 3
  • 49
  • 76
3

You can't dereference a pointer which has a value of NULL. So you won't be able to access *ppPoint.

You can check the address of a variable with the %p formater in a printf() to check.

printf("Address of ppPoint: %p", *ppPoint);
printf("Address of *ppPoint: %p", *ppPoint);
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
cocool97
  • 1,201
  • 1
  • 10
  • 22
3

A null pointer does not point to anything. That applies to ordinary pointers, function pointers and member pointers alike. It also applies to pointers to pointers.

Therefore, it doesn't make sense to talk about the value of "what it points to".

MSalters
  • 173,980
  • 10
  • 155
  • 350
3

First, there's the usual misconceptions about the mysterious null. In this case there's 3 related terms:

  • null pointer
  • null pointer constant
  • The NULL macro

A null pointer constant is the integer 0 or that integer cast to a pointer, (void*)0. The NULL macro is guaranteed to be a portable null pointer constant.

Whenever you assign a null pointer constant, such as NULL, to any pointer, that pointer becomes a null pointer. This allows compilers to internally represent a null pointer as something else than 0, because the address 0x00...00 is quite often a valid physical address on many systems.

Specifically, C17 6.3.2.3/3 defines this:

An integer constant expression with the value 0, or such an expression cast to type void *, is called a null pointer constant. 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.


When accessing a null pointer by de-referencing it, with don't get any predictable result. This is undefined behavior as per C17 6.5.3.2/4:

If an invalid value has been assigned to the pointer, the behavior of the unary * operator is undefined.

Meaning anything can happen. If you are lucky, you just get a system crash.

You can however compare a null pointer against another null pointer, or against a null pointer constant, and they are guaranteed to be equal (C17 6.5.9).

As for pointer-to-pointer being some a special case, it is not. The pointed-at type is irrelevant for any of the above mentioned rules. Overall there is nothing special with pointer-to-pointer in C - it is never a special case, but always to be regarded as pointer-to-type.

Lundin
  • 195,001
  • 40
  • 254
  • 396