C Notes
Master tricky pointer interview questions in C covering pointer arithmetic, pointer to pointer, function pointers, array-pointer relationships, and common pitfalls.
Pointers are the most frequently tested topic in C programming interviews. They separate strong C programmers from beginners. This guide covers the trickiest pointer questions that interviewers love to ask, from basic concepts to mind-bending puzzles involving multiple levels of indirection.
Fundamental Pointer Questions
Q1: What will this code print?
#include <stdio.h>
int main() {
int x = 10;
int *p = &x;
int **pp = &p;
printf("%d %d %d\n", x, *p, **pp);
printf("%p %p %p\n", (void*)&x, (void*)p, (void*)*pp);
return 0;
}10 10 10 0x7ffd1234 0x7ffd1234 0x7ffd1234
Explanation: *p dereferences p to get x's value. **pp dereferences pp to get p, then dereferences p to get x's value. All three access the same memory location.
Q2: Pointer Arithmetic with Different Types
*p = 10 *(p+2) = 30 *p + 2 = 12 ++*p = 11 *p++ = 11 *p = 20
Explanation:
*(p+2)moves pointer 2 positions forward, then dereferences → 30*p + 2dereferences first (gets 10), then adds 2 → 12++*pincrements the value at p (10→11)*p++returns current value (11), then advances pointer to next element- After
p++, p points to arr[1] which is 20
Q3: Array Name vs Pointer
sizeof(arr) = 20 sizeof(ptr) = 8 arr[2] = 3 ptr[2] = 3 2[arr] = 3 *(arr+2) = 3
Explanation: sizeof(arr) gives the total array size (5×4=20). sizeof(ptr) gives pointer size (8 on 64-bit). 2[arr] works because a[b] is defined as *(a+b), and addition is commutative.
Q4: Pointer to Array vs Array of Pointers
Using array of pointers: 10 20 30 Using pointer to array: 10 20 30 sizeof(p1) = 24 sizeof(p2) = 8
Q5: String Pointer Pitfall
str1: Mello str2: Hello sizeof(str1) = 6 sizeof(str2) = 8
Q6: Function Pointer Question
add(3,4) = 7 mul(3,4) = 12 ops[0](5,6) = 11 ops[1](5,6) = 30
Q7: Pointer and const
#include <stdio.h>
int main() {
int x = 10, y = 20;
const int *p1 = &x; // Pointer to constant int
// *p1 = 30; // ERROR: can't modify value
p1 = &y; // OK: can change pointer
int *const p2 = &x; // Constant pointer to int
*p2 = 30; // OK: can modify value
// p2 = &y; // ERROR: can't change pointer
const int *const p3 = &x; // Constant pointer to constant int
// *p3 = 40; // ERROR
// p3 = &y; // ERROR
printf("x = %d, *p1 = %d, *p2 = %d\n", x, *p1, *p2);
return 0;
}x = 30, *p1 = 20, *p2 = 30
Q8: Pointer Subtraction
p2 - p1 = 3 (char*)p2 - (char*)p1 = 12
Explanation: Pointer subtraction gives the number of elements between them (3 elements). Cast to char* gives actual byte difference (3 × 4 = 12 bytes).
Q9: Returning Pointer from Function
#include <stdio.h>
#include <stdlib.h>
// WRONG: Returns pointer to local variable
int* bad_function() {
int x = 42;
return &x; // x is destroyed after function returns!
}
// CORRECT: Returns dynamically allocated memory
int* good_function(int value) {
int *p = (int*)malloc(sizeof(int));
*p = value;
return p; // Caller must free
}
// CORRECT: Returns pointer to static variable
int* static_function() {
static int x = 42;
return &x; // static lives for entire program
}
int main() {
int *result = good_function(99);
printf("Good: %d\n", *result);
free(result);
int *s = static_function();
printf("Static: %d\n", *s);
return 0;
}Good: 99 Static: 42
Q10: void Pointer Operations
Int: 42 Float: 3.14 Char: A
Q11: Double Pointer for Modifying Pointer in Function
#include <stdio.h>
#include <stdlib.h>
void allocate(int **ptr, int value) {
*ptr = (int*)malloc(sizeof(int));
**ptr = value;
}
void wrongAllocate(int *ptr, int value) {
ptr = (int*)malloc(sizeof(int)); // Modifies local copy!
*ptr = value;
// Memory leak - caller's pointer unchanged
}
int main() {
int *p = NULL;
allocate(&p, 42);
printf("After allocate: %d\n", *p);
free(p);
return 0;
}After allocate: 42
Explanation: To modify a pointer inside a function (make it point somewhere new), you need a pointer to that pointer (double pointer).
Q12: Tricky Increment Question
*p++ = 0 *p = 1 (*p)++ = 1 *p = 2 *++p = 2 ++*p = 3
Pointer Interview Tips
| Concept | Key Rule |
|---|---|
*p++ | Dereference, then advance pointer |
(*p)++ | Increment the value pointed to |
*++p | Advance pointer, then dereference |
++*p | Increment value, return new value |
| Array name | Decays to pointer (except sizeof, &) |
const int *p | Can't change value through p |
int *const p | Can't change where p points |
Summary
Pointer questions test your understanding of memory, indirection, and C's type system. Key areas to master: pointer arithmetic (moves by sizeof(type)), pointer-array relationship, function pointer syntax, const correctness with pointers, double pointers for modifying pointers in functions, and the difference between stack and heap allocations. Practice tracing through code manually – draw memory diagrams with boxes and arrows for complex pointer manipulations.
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Pointer Interview Questions in C – Tricky Problems with Answers.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this C Programming topic.
Search Terms
c-programming, c programming, programming, interview, preparation, pointer, questions, pointer interview questions in c – tricky problems with answers
Related C Programming Topics