0

I'm quite new to programming and have been looking at a few example codes, I would just like to know what is meant by the if(!pi) statement in this code.

// allocate and populate array
    int* pi = new int[N];
    if (!pi)
        return 1;
    for (int i = 0; i < N; i++)
        pi[i] = rand() % 100000;
Jarod42
  • 203,559
  • 14
  • 181
  • 302

1 Answers1

6

Pointers are implicitly convertible to bool. If the pointer is null, then the result is false and otherwise true. operator ! is the logical NOT operator.

In this case the check is redundant, because new[] will never return a null pointer and therefore the condition is never true.

eerorika
  • 232,697
  • 12
  • 197
  • 326