Albus Dumbledore
Albus Dumbledore

Reputation: 12616

Would these C-pointer operations cause problems?

Let's say I've got that:

char *p = NULL;

I'm sure this one would be a problem as I'd be dereferencing a NULL pointer:

*p = 16;

On the other hand, I think this one would be OK for I'd be getting the address of *p which is not NULL itself:

char **pp = &p;

Am I right in both cases?

Upvotes: 1

Views: 91

Answers (3)

Furquan
Furquan

Reputation: 678

You are correct in both cases. Just to add a simple clarification. Though you assign

char **pp = &p;

You still cant access **pp, because it is still has NULL. But you can safely access *pp.

Upvotes: 1

MByD
MByD

Reputation: 137382

Yes you are. Although the value of p is NULL, it still has a valid address, so you may pass its reference.

Upvotes: 5

JeremyP
JeremyP

Reputation: 86651

Yes, you are correct in both cases.

Upvotes: 4

Related Questions