🇯🇵 日本語 | 🇺🇸 English
Advertisement

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 fetch 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 to (which is a). The value of a changes, so 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 this triggers a Segmentation Fault. Always check for NULL before dereferencing.

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 values are swapped through pointers. Since *a = x, *b = y: tmp=3, *a=7, *b=3.
Result: x=7, y=3. This is the classic use of pointers as function arguments.

Result

Please answer the questions
Back to Pointer lessonHome