LOGBOOK

HELP

Quiz Entry - updated: 2026.07.10

How do pointers work in C conditions and what is NULL?

A pointer is just a memory address, so in a condition NULL (which is 0) is false and any real address is true — that's why if (a) tests "is this pointer set?".

void foo(int *a, int *b) {
    int c;

    // These three are equivalent:
    if (a != NULL) c = *a;
    if (a != 0)    c = *a;
    // Most idiomatic
    if (a)         c = *a;
}

NULL is defined as 0:

// Common definition
#define NULL ((void*)0)

// Pointer to nothing
int *p = NULL;
if (p) {
    // This won't execute - p is NULL (zero)
}

The ternary operator ?::

// Instead of:
if (a != NULL) c = *a;
else c = 0;

// Write:
c = a ? *a : 0;

Avoid nested ternary:

// Hard to read!
c = a ? *a : b ? *b : 0;

From Quiz: REVE1 / C Programming | Updated: Jul 10, 2026