- How pointers store addresses and how to dereference them
- Pointer arithmetic โ how
p + 1advances bysizeof(*p) - The crucial difference between
*ptr++and(*ptr)++ - How arrays and pointers interact (array decay)
- Double pointers and multi-level indirection
- Function pointers and the declaration syntax
- const pointer vs pointer to const
- Dangling pointers and NULL handling
This page has 40 multiple choice questions on pointers in C. You'll find questions on declaration, arithmetic, array-pointer relationships, pre vs post increment, double pointers, function pointers, and const pointers. Each question comes with the correct answer and a short explanation in plain English, so you understand the "why" and not just the "what". Pointers are the most-tested topic in C interviews and college exams โ trace every code snippet line by line, and pay special attention to the pre vs post increment questions.
Pointer Basics
Q1. What is the output of the given code?
int a = 10;
int *ptr = &a;
printf("%d", *ptr);
Answer: a) 10
ptr stores the address of a. Dereferencing with *ptr gives the value stored there, which is 10. This is the simplest pointer example โ hold an address, then read the value. If you answered "Error", you may have confused *ptr with &ptr; the syntax here is correct.
Q2. What is the output of the following code?
int x = 5;
int y = 10;
int *ptr1 = &x;
int *ptr2 = &y;
printf("%d", *ptr1 + *ptr2);
Answer: a) 15
*ptr1 gives 5 and *ptr2 gives 10, so the sum is 15. This tests two things: dereferencing gives the pointed-to value, and dereferenced pointers can be used in arithmetic expressions just like normal variables. If you answered "5 10", you may have thought the code prints both separately โ but there's only one %d.
Q3. What does the following code snippet do?
int *ptr; ptr = NULL;
Answer: b) Assigns NULL to a pointer, making it a null pointer
Line 1 declares an uninitialized pointer (a wild pointer). Line 2 assigns NULL to it, making it safe to test against. A pointer set to NULL points nowhere, so dereferencing it will crash predictably. This is the standard idiom for "no valid target yet" in C.
Q4. What is the output of the following program?
int arr[5] = {1, 2, 3, 4, 5};
int *ptr = arr;
printf("%d", *ptr);
Answer: a) 1
The array name arr decays to a pointer to its first element, so ptr points to arr[0]. *ptr therefore gives arr[0] = 1. This is one of the most fundamental C idioms โ array name = pointer to first element. If you answered 5, you might have thought ptr points to the last element.
Q5. What is the output of the following code?
int a = 5, b = 10;
int *ptr1 = &a;
int *ptr2 = &b;
*ptr1 = *ptr2;
printf("%d", a);
Answer: b) 10
Line 3 dereferences both pointers to read their values. It reads *ptr2 = 10 and writes it to *ptr1, which modifies a. So a becomes 10. This shows pointers can write to their targets, not just read. b remains 10 as well. If you answered 5, you probably thought *ptr1 = *ptr2 assigns the pointers, not the values they point to.
Q6. What is the output of the following code?
int a = 5;
int *ptr = &a;
*ptr += 2;
printf("%d", a);
Answer: c) 7
*ptr refers to a's memory. *ptr += 2 is equivalent to a = a + 2, so a becomes 7. This is how functions can modify their caller's variables โ pass a pointer, dereference, modify. The pointer indirection lets you reach the original memory from a different scope.
Q7. What is the output of the following code?
int a = 5;
int *ptr = &a;
*ptr--;
printf("%d", a);
Answer: a) 5
This is a classic trap. Due to operator precedence, *ptr-- parses as *(ptr--), meaning "decrement the pointer, then dereference the old pointer value (discarded)". The value at a is never touched! The pointer ptr gets decremented (now points before a in memory, which is undefined), but a stays 5. Compare with Q8 below to see the difference.
Q8. What is the output of the following code?
int a = 5;
int *ptr = &a;
(*ptr)--;
printf("%d", a);
Answer: b) 4
The parentheses force the dereference first: (*ptr) gives a's value, then -- decrements it. So a becomes 4. This is exactly what makes Q7 and Q8 so valuable together โ same characters, different parentheses, different result. The lesson: parenthesise dereference-then-modify.
Pointer Arithmetic & Arrays
Q9. What is the output of the following code?
int arr[5] = {1, 2, 3, 4, 5};
int *ptr = arr;
printf("%d", *(ptr + 2));
Answer: b) 3
ptr points to arr[0]. ptr + 2 points to arr[2] (the third element, since indexing is zero-based). Dereferencing gives 3. Remember: pointer arithmetic uses the size of the pointed-to type โ int is typically 4 bytes, so ptr + 2 advances 8 bytes in actual memory.
Q10. What is the output of the following code?
int arr[5] = {1, 2, 3, 4, 5};
int *ptr = &arr[3];
printf("%d", *ptr);
Answer: c) 4
arr[3] is the fourth element (index 3), which is 4. ptr points to it, so *ptr is 4. This tests the offset between "third element" (index 2) and "index 3" โ a very common point of confusion. The exam answer "4" is correct because the code uses &arr[3], not arr[2].
Q11. What is the output of the following code?
int arr[5] = {1, 2, 3, 4, 5};
int *ptr = arr;
printf("%d", *ptr + 2);
Answer: c) 3
Read this carefully: *ptr + 2 is parsed as (*ptr) + 2, not *(ptr + 2). Because unary * binds tighter than binary +. So *ptr is 1, and 1 + 2 = 3. This is completely different from *(ptr + 2), which would also give 3 here โ but for a different reason (that one uses pointer arithmetic). A great exercise is to try *ptr + 2 with ptr pointing to arr[2] to see the difference.
Q12. What is the output of the following code?
int arr[5] = {1, 2, 3, 4, 5};
int *ptr = &arr[1];
printf("%d", *ptr - 1);
Answer: b) 1
ptr points to arr[1] = 2. *ptr - 1 = 2 - 1 = 1. Note this subtracts 1 from the value, not from the pointer. This question deliberately mirrors the previous one to reinforce: *ptr - 1 uses the value; ptr - 1 uses pointer arithmetic. Watch out for this in exams.
Q13. What is the output of the following code?
int arr[5] = {1, 2, 3, 4, 5};
int *ptr = arr + 2;
printf("%d", *ptr);
Answer: c) 3
arr + 2 points to arr[2], which is the third element, 3. This is the same result as writing ptr = &arr[2]. Two ways, same address. Many C programmers prefer ptr = arr + n; it makes the arithmetic explicit and avoids the arr[n] notation if you want to walk a pointer.
Q14. What is the output of the following code?
int arr[5] = {1, 2, 3, 4, 5};
int *ptr = arr + 3;
printf("%d", *ptr - 2);
Answer: b) 2
ptr = arr + 3 points to arr[3] = 4. Then *ptr - 2 = 4 - 2 = 2. Notice *ptr is dereferenced first, then 2 is subtracted from the resulting value. This is not "two elements before ptr" โ that would be *(ptr - 2).
Q15. What is the output of the following code?
int arr[5] = {1, 2, 3, 4, 5};
int *ptr = arr;
printf("%d", *(ptr += 2));
Answer: c) 3
ptr += 2 moves the pointer itself to point to arr[2], and the expression returns the new pointer (unlike postfix ++). So the whole expression is *(arr + 2) = 3. The pointer ptr is permanently advanced after this statement. If you use ptr again later, it points to arr[2], not arr[0].
Q16. What is the output of the following code?
int arr[5] = {1, 2, 3, 4, 5};
int *ptr = &arr[0];
printf("%d", *(ptr + 4));
Answer: d) 5
ptr + 4 points to arr[4], the last element, which is 5. This is the standard way to reach the end of a 5-element array: start at arr[0] and walk 4 steps forward. In pointer terms, one-past-the-end would be arr + 5, but that address is not dereferenceable.
Q17. What is the output of the following code?
int arr[5] = {1, 2, 3, 4, 5};
int *ptr = &arr[0];
printf("%d", *(ptr + 1) - *ptr);
Answer: b) 1
*(ptr + 1) = arr[1] = 2, and *ptr = arr[0] = 1. So 2 โ 1 = 1. This pattern โ difference between consecutive elements โ is exactly how you find the gap in a sorted array, or compute the step between values. Notice the difference from *(ptr + 1) - *(ptr - 1), which would be 2 โ 0 = 2.
Q18. What is the output of the following code?
int arr[5] = {1, 2, 3, 4, 5};
int *ptr = arr + 3;
printf("%d", *ptr - *(ptr - 1));
Answer: b) 1
ptr = arr + 3, so *ptr = 4. *(ptr - 1) points to arr[2] = 3. So 4 โ 3 = 1. This is the classic "difference between adjacent array elements" pattern. It appears in problems like finding a missing number, checking arithmetic progressions, and detecting anomalies.
Pre vs Post Increment
Q19. What is the output of the following code?
int arr[5] = {1, 2, 3, 4, 5};
int *ptr = arr;
printf("%d", *(ptr++));
Answer: a) 1
Postfix ++ returns the old value of the pointer (arr), then increments. So *(ptr++) is *arr = 1. After the statement, ptr points to arr[1]. Compare with *++ptr, which would give arr[1] = 2. Postfix returns old, prefix returns new โ same rule as with normal integers.
Q20. What is the output of the following code?
int arr[5] = {1, 2, 3, 4, 5};
int *ptr = arr;
printf("%d", *(ptr)++);
Answer: a) 1
Here the parentheses mean (*ptr) is incremented as a value, not as a pointer. So *ptr reads 1, then arr[0] becomes 2. The printed value is the old value 1. The array is modified: arr[0] = 2 after this. Only the printed value is what the exam is asking about.
Q21. What is the output of the following code?
int arr[5] = {1, 2, 3, 4, 5};
int *ptr = &arr[2];
printf("%d", *ptr++);
Answer: c) 3
ptr points to arr[2] = 3. Postfix ++ returns the current pointer (arr[2]), then advances to arr[3]. So *ptr++ gives 3, then ptr points to arr[3]. After this line, *ptr would be 4. The value printed is 3.
Q22. What is the output of the following code?
int arr[5] = {1, 2, 3, 4, 5};
int *ptr = arr + 1;
printf("%d", *(ptr--));
Answer: b) 2
ptr points to arr[1] = 2. Postfix -- returns the old pointer (arr[1]), then decrements to arr[0]. So *(ptr--) gives 2, and after the statement ptr points to arr[0]. Same postfix-returns-old rule as ++, just in reverse.
Q23. What is the output of the following code?
int arr[5] = {1, 2, 3, 4, 5};
int *ptr = arr + 2;
printf("%d", *(ptr--));
Answer: c) 3
ptr points to arr[2] = 3. Postfix -- returns the old pointer (arr[2]), then decrements to arr[1]. So *(ptr--) gives 3, and after the statement ptr points to arr[1]. This question tests the same postfix rule with a different starting position.
Q24. What is the output of the following code?
int arr[5] = {1, 2, 3, 4, 5};
int *ptr = arr + 2;
printf("%d", *ptr++);
Answer: c) 3
Same as Q21, but with the parens removed. *ptr++ parses as *(ptr++) โ dereference the old pointer then advance. Since ptr starts at arr[2], the old value is 3, and afterwards ptr points to arr[3]. The printed output is 3. This shows that *ptr++ and *(ptr++) are equivalent โ both are the same due to operator precedence.
Q25. What is the output of the following code?
int arr[5] = {1, 2, 3, 4, 5};
int *ptr = &arr[2];
printf("%d", *ptr--);
Answer: c) 3
ptr points to arr[2] = 3. *ptr-- parses as *(ptr--) โ dereference old pointer, then decrement. So it prints 3, then ptr points to arr[1]. Same pattern as before; the answer is 3.
Q26. What is the output of the following code?
int arr[5] = {1, 2, 3, 4, 5};
int *ptr = arr;
printf("%d", *++ptr - *ptr);
Answer: b) 1 (but this expression has undefined behaviour in strict C)
In practice, *++ptr increments ptr to arr[1] and returns arr[1] = 2. Then *ptr also reads arr[1] = 2. So 2 โ 2 = 0 would be a typical expectation โ but many compilers evaluate left-to-right and see 2 โ 2 = 0.
Wait โ this is actually a known tricky case. The C standard says reading *ptr and *++ptr in the same expression that modifies ptr has undefined behaviour. On most compilers you'll get 0 (both read arr[1]), but the answer is unpredictable. The exam answer "b) 1" is technically wrong for this reason. Avoid writing code like this in real life.
Q27. What is the output of the following code?
int arr[5] = {1, 2, 3, 4, 5};
int *ptr = arr;
printf("%d", (*ptr)++);
Answer: a) 1
(*ptr)++ dereferences to arr[0] = 1, returns the old value 1, then increments arr[0] to 2. So the output is 1 and the array changes. After this statement, ptr still points to arr[0], but arr[0] is now 2. This is the classic "postfix on the value, not the pointer" pattern.
Q28. What is the difference between *ptr++ and (*ptr)++ in C?
Answer: b) *ptr++ increments the pointer; (*ptr)++ increments the value pointed to
Because postfix ++ has higher precedence than unary *, *ptr++ parses as *(ptr++): it dereferences the old pointer then advances the pointer. With parentheses, (*ptr)++ forces the dereference first, so it increments the pointed-to value. This single distinction trips up beginners in dozens of exam questions, so memorise it.
Pointer Swapping & Values
Q29. What is the output of the following code?
int a = 5, b = 10;
int *ptr1 = &a;
int *ptr2 = &b;
int temp = *ptr1;
*ptr1 = *ptr2;
*ptr2 = temp;
printf("%d", a);
Answer: b) 10
This is the classic swap-via-pointers pattern. temp holds 5; *ptr1 gets 10 (from b); *ptr2 gets 5 (from temp). After this, a = 10 and b = 5 โ they've been swapped. The question only asks about a, which is 10. This pattern is why functions like void swap(int *a, int *b) work in C.
Double Pointers & Function Pointers
Q30. What is the output of the following code?
int a = 5;
int *ptr = &a;
int **ptr2 = &ptr;
printf("%d", **ptr2);
Answer: a) 5
ptr2 points to ptr, which points to a. Two dereferences: *ptr2 gives ptr, **ptr2 gives the value at ptr, which is a = 5. This "pointer to pointer" pattern is used whenever a function needs to modify a pointer passed by the caller โ for example, in void allocate(int **arr, int n).
Q31. How do you declare a pointer to a function that takes no arguments and returns an integer in C?
Answer: d) int (*ptr)(void);
This is the strictest form. The parentheses around *ptr are essential โ without them, int *ptr(void) declares a function returning int*, not a pointer to a function. Adding void inside the parameter list makes "takes no parameters" explicit, which is why d is preferred over a for exam answers. Both a and d compile in practice; d is the cleaner form.
Q32. What is the correct syntax to declare a pointer to an array of 5 ints?
Answer: b) int (*p)[5];
The parentheses around *p are essential. Without them, int *p[5] declares an array of 5 pointers to int (very different!). The correct declaration reads "p is a pointer to an array of 5 ints". This distinction appears in almost every advanced pointer exam โ using p + 1 with int (*p)[5] advances by 5 ints, not 1.
Q33. What is the output of the following code?
int arr[10] = {1,2,3,4,5,6,7,8,9,10};
printf("%d", sizeof(arr)/sizeof(arr[0]));
Answer: b) 10
sizeof(arr) is the total bytes of the array (typically 40 for 10 ints), and sizeof(arr[0]) is 4 (one int). 40 รท 4 = 10, the number of elements. This idiom computes the array's length โ but it only works when arr is a real array in scope, not when passed to a function (where it has already decayed to a pointer).
Q34. What does the array name decay to when passed to a function?
Answer: b) A pointer to the first element
This is "array decay". When you pass an array to a function, the compiler converts it to a pointer to its first element. That's why the function needs the size as a separate argument โ inside the function, sizeof(arr) gives the pointer size (usually 8), not the array size. This rule explains why arrays and pointers feel interchangeable in C.
Q35. Can you do pointer arithmetic on a void pointer in standard C?
Answer: b) No, standard C forbids it because void has no size
GCC allows void* arithmetic as an extension (treating it as 1-byte units like char*), but the C standard doesn't define this. For portability, cast to char* before doing arithmetic: (char*)ptr + n. This is a subtle but important portability issue that comes up in real-world C code.
const Pointers & Advanced Concepts
Q36. What is the difference between const int *p and int * const p?
Answer: b) const int *p: value can't change; int * const p: pointer can't change
Read declarations right-to-left. const int *p = "p is a pointer to a const int" โ the pointed-to value is read-only, but the pointer can be re-pointed. int * const p = "p is a const pointer to int" โ the pointer is fixed, but the value can change. This distinction is very common in API design and interview questions.
Q37. What is a dangling pointer?
Answer: c) A pointer to memory that has been freed or gone out of scope
Dangling pointers point to memory that no longer belongs to you โ either because it was freed (with free()) or because a local variable went out of scope. Reading or writing through a dangling pointer is undefined behaviour. The best prevention: set the pointer to NULL after free, and never return pointers to local variables.
Q38. What happens if you dereference a null pointer?
Answer: c) Undefined behaviour, typically a segmentation fault / crash
Dereferencing NULL is undefined behaviour. On most systems it triggers a segmentation fault because address 0 is protected by the OS โ a "predictable crash." Some embedded systems may have memory at address 0, in which case you get arbitrary behaviour. Always guard with if (ptr != NULL) before dereferencing.
Q39. What happens when you free the same pointer twice?
Answer: b) Undefined behaviour โ may crash or corrupt the heap
Double-free corrupts the heap manager's internal bookkeeping, which can lead to mysterious bugs in unrelated code. Set the pointer to NULL after free โ free(NULL) is safely a no-op, so this prevents accidental double-free. Modern allocators sometimes catch double-free and print an error, but you shouldn't rely on that.
Q40. Which of the following is equivalent to arr[i] in C?
Answer: a) *(arr + i)
This is the fundamental definition of array indexing in C: arr[i] is exactly *(arr + i). The compiler literally translates one into the other. Because of this definition, i[arr] also works (it expands to *(i + arr)), which is a fun trivia bit. Option b) *arr + i would be (arr[0]) + i โ the value at index 0, plus i, which is very different. This equivalence is the key insight that makes pointers and arrays feel interchangeable in C.
Frequently Asked Questions
Why is the output of *ptr++ different from (*ptr)++?
Because of operator precedence. Postfix ++ has higher precedence than unary *, so *ptr++ is parsed as *(ptr++): it returns the old pointed-to value, then increments the pointer. (*ptr)++ forces the dereference first, so it returns the old value at the pointer, then increments the value pointed to. Same characters, different parentheses, different result.
What is pointer arithmetic, and why does p + 1 advance by sizeof(*p) bytes?
Pointer arithmetic is defined in units of the pointed-to type. p + 1 means "the next element", whose byte distance is sizeof(*p). For int* on a 4-byte-int system, that's 4 bytes; for char*, 1 byte. This is why pointer types matter even though all pointers have the same size.
What is the difference between arr[i], *(arr + i), and i[arr]?
All three are identical. arr[i] is defined as *(arr + i), and *(arr + i) equals *(i + arr) by commutativity, so i[arr] also works. Only the first is idiomatic; use arr[i] in real code.
Why can't I do pointer arithmetic on a void pointer?
Because void has no size, the compiler can't determine how far to advance. GCC allows it as an extension (treating void* like char*), but standard C forbids it. Cast to char* before doing arithmetic for portability.
What happens when you free the same pointer twice?
Double-free is undefined behaviour. It may crash immediately, silently corrupt the heap, or appear to work until a later crash. Set the pointer to NULL after free(); free(NULL) is a safe no-op.
๐ก Tip: Pointers are the most-tested topic in C exams and interviews. Focus on these five patterns: *ptr++ vs (*ptr)++, pointer arithmetic with arrays, array decay, double pointers, and const pointer syntax. If you can trace those confidently, you'll handle almost any pointer question.