2

I am currently learning C++ and have gotten to pointers. I am struggling to understand the difference between initializing a pointer to zero and initializing it to nullptr, as demonstrated below.

int *p = 0;
int *p1 = nullptr;

My understanding is that p and p1 are both null pointers. Does this mean that nullptr is equivalent to zero, and that both p and p1 are pointing to the same address (that is, the address for zero)?

If they are the same, why would I want to use nullptr when I could just use zero? If they aren't the same, why is it useful to have multiple ways to declare a null pointer instead of one way?

Dez M
  • 57
  • 5
  • In this case there is no difference between the pointers, both are null. – HolyBlackCat Mar 21 '19 at 22:17
  • template void Fwd(F f, A a){f(a);} void func(int* i){} ##### func(NULL)// Fine ##### func(0);// Fine// ##### Fwd(func, nullptr);// Fine ##### Fwd(func, NULL); **// ERROR: No function func(int)** – Jon Goodwin Mar 21 '19 at 22:23

0 Answers0