5

In C++,we usually check a pointer whether is null or not, I just know we should use

if(NULL == ptr)

instead of:

if(ptr == NULL)

I want to know why?

In additiol, if we want to initialized a ptr to null,should we use ptr = NULL or ptr = 0? yes I know in C++, we usually use ptr = nullptr, I want to know why shall we do like this just want to unify the code ? thanks

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
minicaptain
  • 1,196
  • 9
  • 16

2 Answers2

12

It's a coding style (named Yoda Conditions) to avoid writing = instead of == in an if-statement, it's valid to use assignment = in an if-statement, but it's usually not what you want.

Personally, I prefer not to use like this because it's hard to read and modern compilers will warn you when you use = in an if-statement.

Also note that:

  1. if(ptr == NULL) is the same with if(!ptr).
  2. C++11 introduced nullptr to replace using NULL. So to initialize a null pointer, it's preferred to use ptr = nullptr

About why use nullptr over NULL:

Before C++11, NULL is usually implemented internally as #define NULL 0, but the problem is, 0 is also the integer zero. It may cause trouble in some situations. For example:

void func(int n);   
void func(char *s);

func(NULL);   //call which function?

Though the auther implies that NULL is a pointer type, but the compiler just know to call func(0). So the first version will be called.

Using func(nullptr), the compiler will know it's a pointer and call the second version.

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
  • +1, but I would point out that the problem is that `=` is *assignment* even in a conditional - this often confuses beginners. This is always assumed in this answer, but never stated explicitly. – Matteo Italia Jul 21 '13 at 12:51
  • 1
    It’s `nullptr`, not `null_ptr`. – Konrad Rudolph Jul 21 '13 at 12:52
  • yes in my second question, I know we should use nullptr, I want to know why? just want to unify the code style? – minicaptain Jul 21 '13 at 12:56
  • @minicaptain You mean you don't understand why use `nullptr` over `NULL` or why to initialize a pointer to null pointer? – Yu Hao Jul 21 '13 at 12:58
  • I want to know why use nullptr ove null,thanks! – minicaptain Jul 21 '13 at 12:59
  • @minicaptain `nullptr` was introduced for better type safety. For example, given two functions `void foo(int i)` and `void foo(char* str)`, then the call `foo(NULL)` is not valid in C++ because it's ambiguous. Even though you know that want to call the `foo(char*)` overload, the compiler can't know that. On the other hand, `foo(nullptr)` is perfectly fine, since `nullptr` is only valid a parameter to `foo(char*)` and not `foo(int)`. – Nikos C. Jul 21 '13 at 13:08
  • As to modern compilers spotting that mistake - this is not true - sometimes they miss it! – Ed Heal Jul 21 '13 at 13:30
1

Using yoda equality it prevents certain mistakes such as using a single = or where the compiler tries to determine an integer from a smart pointer.

It has been known

I spent two days tracking down that error as they think yoda is a bad idea.

Ed Heal
  • 59,252
  • 17
  • 87
  • 127