🇯🇵 日本語 | 🇺🇸 English

Quiz (Pointers)

Test your understanding of the & operator, * operator, NULL pointers, and the swap function.

Question 1 — The & operator

int a = 42;
int *p = &a;
printf("%d\n", *p);

What does this print?

42
An address
0
Compile error
Explanation: p holds the address of a. *p dereferences the pointer to read the value at that address — the value of a, which is 42.

Question 2 — Modifying through a pointer

int a = 10;
int *p = &a;
*p = 20;
printf("a = %d\n", a);

What does this print?

a = 10
a = 20
a = 0
Compile error
Explanation: *p = 20 writes 20 to the location p points at (which is a). The value of a changes, so we get a = 20.
Pointers let you modify a variable indirectly.

Question 3 — NULL pointer

int *p = NULL;
printf("%d\n", *p);

What happens?

0
NULL
Runtime error (Segmentation Fault)
Compile error
Explanation: Dereferencing a NULL pointer (*p) is undefined behavior.
On most systems it triggers a segmentation fault. Always check for NULL before dereferencing a pointer.

Question 4 — swap function

void swap(int *a, int *b) {
    int tmp = *a;
    *a = *b;
    *b = tmp;
}
int main(void) {
    int x = 3, y = 7;
    swap(&x, &y);
    printf("x=%d, y=%d\n", x, y);
    return 0;
}

What does this print?

x=3, y=7
x=7, y=3
x=7, y=7
Compile error
Explanation: The two values are swapped through pointers. Since *a = x and *b = y: tmp=3, *a=7, *b=3.
Result: x=7, y=3. This is the classic use of pointers as function arguments.

Question 5 — Pointers and arrays

int a[3] = {10, 20, 30};
int *p = a;
printf("%d\n", p[1]);

What does this print?

10
20
30
Compile error
Explanation: Inside an expression, the array name a is treated as a pointer to the first element.
  • p = a makes p point to a[0]
  • p[1] means the same thing as *(p + 1), which equals a[1] = 20
The fact that the subscript operator [] also works on pointers reflects how closely pointers and arrays are related.

Question 6 — Pointer arithmetic

int a[4] = {1, 2, 3, 4};
int *p = a;
p++;
printf("%d\n", *p);

What does this print?

1
2
3
An address value
Explanation: Applying ++ to a pointer advances the address by the size of the pointed-to type.
  • Initially, p points at a[0] (=1)
  • After p++, p points at a[1] (it moves forward by sizeof(int) = 4 bytes)
  • *p = a[1] = 2
An int* advances by sizeof(int); a char* advances by 1 byte. This is the foundational technique for walking through an array with pointers.

Question 7 — NULL check

void safe_print(int *p) {
    if (p != NULL) {
        printf("%d\n", *p);
    } else {
        printf("NULL\n");
    }
}
int main(void) {
    safe_print(NULL);
    return 0;
}

What does this print?

0
NULL
Segmentation fault
Compile error
Explanation: In any function that takes a pointer, a NULL check matters.
  • Even when p is NULL, the if (p != NULL) guard prevents *p from running
  • Control falls through to the else branch and "NULL" is printed
Dereferencing *p without a NULL check would segfault. Guarding with an if is an essential habit for writing safe C code.

Question 8 — Dangling pointer

int *danger(void) {
    int x = 42;
    return &x;  // returns the address of a local variable
}
int main(void) {
    int *p = danger();
    printf("%d\n", *p);
    return 0;
}

What's the problem here?

Always prints 42
Undefined behavior (the local variable is invalidated when the function returns)
Compile error
No problem
Explanation: The local variable x is released from the stack the moment the function returns.
  • Even if you return its address, x's lifetime is over by the time the caller uses it
  • Accessing *p dereferences a dangling pointer — undefined behavior
  • You might happen to see 42, but that's not guaranteed
Fixes: mark x as static, allocate dynamically with malloc, or accept a buffer from the caller as an argument.

Question 9 — Assignment between pointers

int x = 1, y = 2;
int *p = &x;
int *q = &y;
p = q;
*p = 99;
printf("x=%d, y=%d\n", x, y);

What does this print?

x=99, y=2
x=1, y=99
x=99, y=99
x=1, y=2
Explanation: p = q makes p point to the same address as q (it copies the pointer, not the value it points to).
  • After p = q, p points to y (no longer to x)
  • *p = 99 writes 99 to y
  • x is unchanged and stays 1
Note the difference between p = q (pointer assignment) and *p = *q (copying the pointed-to value) — they do very different things.

Result

Answer all the questions to see your score.
Back to the pointers lessonHome