C Pointers Quiz
Questions: 16 · 10 minutes
1. If p has type int *, which statement idiomatically allocates enough space for five int elements?
Scale by the pointed-to type: p = malloc(5 * sizeof *p);
Request only five bytes: p = malloc(5);
Use the size of the pointer: p = malloc(sizeof p);
Use five pointer-address sizes: p = malloc(5 * sizeof &p);
2. Given the declaration int n;, which operation obtains the memory address of n?
Dereference n with *n
Apply array notation with n[]
Measure the object using sizeof n
Apply the address operator as &n
3. A function is declared void swap(int *x, int *y);. Which call lets it exchange the values of int variables a and b?
Use swap(*a, *b), attempting to dereference the integers
Use swap(a, b), passing the integer values directly
Use swap(&a, &b), passing both variable addresses
Use swap(&a, b), passing one address and one value
4. Suppose int *p points to n, and void reset(int **slot) sets *slot to NULL. Which call allows reset to change p itself?
Call reset(p), passing the address stored in p
Call reset(*p), passing the integer reached through p
Call reset(&p), passing the address of the pointer variable
Call reset(&n), passing the address of the integer
5. Which statement correctly describes a null pointer?
It always points to memory containing the integer zero.
It points to no object; it may be compared with NULL but must not be dereferenced.
It points to the first element of an empty array.
It may be dereferenced safely, but its stored value cannot be changed.
6. For int a[6];, what is the result of &a[4] - &a[1]?
The address of a[3]
The size of three int objects in bytes
4
3
7. Given struct Node { int value; struct Node *next; }; and struct Node *cur;, how should cur be updated to point to the following node?
Assign the next member through the pointer: cur = cur->next;
Dereference before applying an invalid member form: cur = *cur.next;
Use direct structure-member syntax on the pointer: cur = cur.next;
Store the address of the pointer variable itself: cur = &cur;
8. In a C program that needs p to store the address of an int, which declaration is correct?
Use the pointer declaration int *p;
Declare an ordinary integer with int p;
Use the C++ reference syntax int &p;
Place the star after the name as int p*;
9. What does the declaration const int *p; allow?
Changing the pointed-to int through p, but not redirecting p
Neither redirecting p nor changing the pointed-to int by any means
Redirecting p while preventing modification of the pointed-to int through p
Only storing a null pointer value in p
10. Consider int n = 12; int *p = &n;. How can the value 12 be read through p?
Use p itself to obtain the stored address
Use &p to obtain the address of the pointer
Dereference the pointer with *p
Attempt to dereference the integer with *n
11. Consider int n = 25; void *vp = &n;. Which expression correctly reads n through vp?
Cast vp to int * and dereference it with *(int *)vp
Convert the pointer value directly to int with (int)vp
Dereference the void pointer directly with *vp
Take the address of the void-pointer variable with &vp
12. Given int a[] = {10, 20, 30, 40}; int *p = a;, what does *(p + 2) evaluate to?
20
30
The address of a[2]
A location two bytes beyond a[0]
13. After calling free(p) on a successfully allocated block, what best describes p if it has not been reassigned?
It automatically becomes NULL.
It is a dangling pointer whose stored address must not be dereferenced.
It remains valid until the current function returns.
It points to a new zero-initialized block.
14. What does second(values) return here? int second(const int *p) { return p[1]; } int values[] = {3, 7, 11};
3
The address of values[1]
11
7
15. What is the value of n after this code runs? int n = 4; int *p = &n; *p = 9;
4
The address stored in p
n is unchanged because only p changes
9
16. A local pointer is declared as int *p; and is not initialized. What should the program do before using *p?
Dereference p once so C can determine its target
Assign p a valid address before dereferencing it
Call free(p) to initialize it as a null pointer
Assume p points to an integer initialized to zero