Pointer Interview Questions in C
If an interviewer wants to know how well you really understand C, they ask about pointers. Pointer questions test whether you can picture memory: what an address is, what * and & do, how arrays and pointers differ and which pointer bugs crash programs.
This page collects the most asked pointer questions with short answers and verified programs. Draw a memory diagram for each one; that habit alone will make you confident in pointer rounds. For the full theory, see Pointers in C.
What You Will Learn
- The core pointer questions and their one-line answers.
- How pointer arithmetic depends on the data type.
- Arrays vs pointers: what is the same and what is different.
- The three const pointer combinations.
- Double pointers and function pointers.
- Pointer bugs interviewers love: NULL, dangling, wild, memory leak.
Pointer Interview Questions Explained
Exam definition: A pointer is a variable whose value is the memory address of another object. The address-of operator & gives an object's address, and the indirection (dereference) operator * accesses the object at an address. Adding an integer n to a pointer moves it by n × sizeof(pointed-to type) bytes.
Memory trick: A pointer is a house address written on a paper. &house = "write down this house's address". *paper = "go to the address on the paper and open the door". paper + 1 = "the next house", and how far that is depends on how wide each house is (the data type).
Core Pointer Questions
- 1. What is a pointer? How do you declare one?
- A variable that stores an address.
int *p;declares a pointer to int;p = &x;stores x's address. - 2. What do & and * do?
&xgives the address of x;*pgives (or changes) the value stored at the address in p.- 3. What is the size of a pointer?
- It depends on the system, not the pointed type: usually 8 bytes on 64-bit and 4 bytes on 32-bit systems.
sizeof(int *)equalssizeof(char *)on common systems. - 4. What happens with p + 1 for an int pointer?
- It points to the next int, i.e. the address increases by sizeof(int) (usually 4), not by 1.
- 5. What is the difference between *p++ and (*p)++?
*p++uses the value at p, then moves p forward;(*p)++increments the value p points to.- 6. What is a double pointer?
- A pointer to a pointer (
int **pp = &p;). Used to change a pointer inside a function, and for 2D dynamic arrays andargv.
Example 1: Pointer Arithmetic and Double Pointers
#include <stdio.h>
int main(void)
{
int a[4] = {10, 20, 30, 40};
int *p = a;
int x = 5, *q = &x, **pp = &q;
char s[] = "ABC";
char *c = s;
printf("*p = %d, *(p + 2) = %d\n", *p, *(p + 2));
printf("int step = %d bytes, char step = %d byte\n",
(int) ((char *) (p + 1) - (char *) p), (int) ((c + 1) - c));
printf("elements between &a[3] and &a[0]: %d\n", (int) (&a[3] - &a[0]));
x = *p++; /* read a[0], then move p */
printf("*p++ gave %d, now *p = %d\n", x, *p);
(*p)++;
printf("after (*p)++: a[1] = %d\n", a[1]);
x = 5;
**pp = 50; /* x through two levels */
printf("x = %d, *q = %d, **pp = %d\n", x, *q, **pp);
return 0;
}
Output
*p = 10, *(p + 2) = 30 int step = 4 bytes, char step = 1 byte elements between &a[3] and &a[0]: 3 *p++ gave 10, now *p = 20 after (*p)++: a[1] = 21 x = 50, *q = 50, **pp = 50
Code Explanation
*(p + 2)is the third element, 30:p + 2moves two ints forward.- Converting to
char *measures bytes: one int step is 4 bytes here, one char step is 1 byte. - Subtracting two pointers into the same array gives the number of elements between them (3), not bytes.
x = *p++reads 10 (a[0]) and then moves p to a[1], so the next*pis 20. The increment is kept in its own statement: usingp++and*pas two arguments of the same printf would be undefined behavior.(*p)++changes the value: a[1] becomes 21.**ppfollows two addresses (pp → q → x), so writing 50 changes x.
Arrays, const and Function Pointers
- 7. Is an array name a pointer?
- No. In most expressions it decays to a pointer to its first element, but
sizeof arrgives the whole array size and you can't assign to an array name. - 8. What is the difference between const int *p, int *const p and const int *const p?
const int *p: can't change the value through p, but p can move.int *const p: p can't move, but the value can change.const int *const p: neither.- 9. What is a function pointer?
- A pointer that stores the address of a function, e.g.
int (*op)(int, int) = add;thenop(2, 3). Used for callbacks (qsort) and menus. - 10. What is the difference between char *s = "hi" and char s[] = "hi"?
char *spoints to a read-only string literal (modifying it is undefined);char s[]is a modifiable array copy of 3 bytes.
Example 2: sizeof, const Pointers and Function Pointers
#include <stdio.h>
int add(int a, int b) { return a + b; }
int mul(int a, int b) { return a * b; }
void show_size(int arr[]) /* arr is really an int * here */
{
printf("inside function: sizeof arr = sizeof(int *) -> %s\n",
sizeof arr == sizeof(int *) ? "yes" : "no");
}
int main(void)
{
int nums[5] = {1, 2, 3, 4, 5};
int x = 10, y = 20;
const int *pc = &x; /* value read-only through pc */
int *const cp = &x; /* pointer fixed */
int (*ops[2])(int, int) = {add, mul};
char word[] = "cat";
printf("sizeof nums = %zu bytes (%zu elements)\n", sizeof nums, sizeof nums / sizeof nums[0]);
show_size(nums);
pc = &y; /* OK: pc may point elsewhere */
*cp = 99; /* OK: value may change */
printf("*pc = %d, x = %d\n", *pc, x);
printf("ops[0](6, 7) = %d, ops[1](6, 7) = %d\n", ops[0](6, 7), ops[1](6, 7));
word[0] = 'b'; /* fine: array copy */
printf("word = %s\n", word);
return 0;
}
Output
sizeof nums = 20 bytes (5 elements) inside function: sizeof arr = sizeof(int *) -> yes *pc = 20, x = 99 ops[0](6, 7) = 13, ops[1](6, 7) = 42 word = bat
- In main,
sizeof numsis the whole array (5 × 4 = 20). Inside the function, the parameter is just a pointer, so you must pass the length separately. pccan be pointed at y, but*pc = 5;would not compile.cpcan change x's value, butcp = &y;would not compile.- An array of function pointers chooses an operation by index, a neat replacement for a switch.
- Modifying
wordis safe because it is an array, not a string literal.
Pointer Bugs Interviewers Ask About
- 11. What is a NULL pointer?
- A pointer set to
NULLto mean "points to nothing". Always checkif (p != NULL)before using it, e.g. after malloc. - 12. What is a wild pointer?
- An uninitialised pointer containing a garbage address. Initialise every pointer (to NULL or a valid address).
- 13. What is a dangling pointer? Give two causes.
- A pointer to memory that is no longer valid: (1) using it after
free, (2) returning the address of a local variable. - 14. What is a memory leak?
- Losing the last pointer to malloc'd memory without freeing it, e.g.
p = malloc(...); p = NULL; - 15. Can you do arithmetic on a void pointer?
- Not in standard C, because the size of void is unknown. Cast it to a typed pointer first (GCC allows it as an extension).
int *bad(void)
{
int local = 5;
return &local; /* WRONG: local disappears when bad() returns -> dangling */
}
int *p; /* WRONG: wild pointer, never initialised */
*p = 10; /* writes to a random address */
int *q = malloc(sizeof *q);
free(q);
*q = 3; /* WRONG: use after free (dangling) */
q = NULL; /* RIGHT habit: set to NULL after free */
Important Points to Remember
- & = address of, * = value at address.
- p + n moves by n × sizeof(type); pointer − pointer gives elements.
- Pointer size depends on the system (usually 8 bytes on 64-bit), not the type.
- An array name decays to a pointer, but it is not a pointer (sizeof, assignment).
const int *pprotects the value;int *const pprotects the pointer.- Avoid wild (uninitialised), dangling (freed/local) and leaked pointers; set pointers to NULL after free.
Common Mistakes Beginners Make
| Mistake | What happens | Fix |
|---|---|---|
int *p, q; expecting two pointers | Only p is a pointer; q is an int | int *p, *q; |
| Using sizeof on an array parameter | Gives pointer size, not array size | Pass the length as a parameter |
Printing an address with %d | Wrong or truncated output | Use %p with (void *)p |
| Returning a local variable's address | Dangling pointer | Return a value, use static or malloc |
Modifying a string literal through char * | Crash on many systems | Use char s[] = "..." |
Pointer Interview Practice Questions
- If
int a[] = {2, 4, 6}; int *p = a;, what is*(p + 1) + 1? - What is printed by
printf("%d", sizeof(char *) == sizeof(double *));on a typical PC? - Which of these is allowed for
const int *p:p++or(*p)++? - Why do we pass
int **to a function that allocates memory for the caller's pointer? - What is the difference between a NULL pointer and a void pointer?
Show answers
- 5 (the second element is 4, plus 1).
- 1: all object pointers usually have the same size.
p++is allowed;(*p)++is not, because the value is const through p.- So the function can change the caller's pointer itself (store the new address in it), not just a copy.
- A NULL pointer is a pointer value meaning "no address"; a void pointer is a pointer type that can hold any address. A void pointer can be NULL.
Coding Exercise
Write void reverse(int *start, int *end) that reverses an array using only pointers (no indexes), by swapping *start and *end and moving both inwards. Reverse {1, 2, 3, 4, 5, 6}.
Show solution
#include <stdio.h>
void reverse(int *start, int *end)
{
int t;
while (start < end)
{
t = *start;
*start = *end;
*end = t;
start++;
end--;
}
}
int main(void)
{
int a[] = {1, 2, 3, 4, 5, 6};
int n = sizeof a / sizeof a[0], *p;
reverse(a, a + n - 1);
for (p = a; p < a + n; p++)
printf("%d ", *p);
printf("\n");
return 0;
}
6 5 4 3 2 1
Quick Quiz: Pointer Interview Questions (MCQ)
-
For
int *ppointing to a[0],p + 3points to:A) a[0] + 3 bytes B) a[3] C) a[1] D) Garbage
-
A pointer that has not been initialised is called a:
A) NULL pointer B) Void pointer C) Wild pointer D) Far pointer
-
In
int *const p = &x;, which is not allowed?A)
*p = 5B)p = &yC) Reading*pD)printf("%d", *p) -
&a[4] - &a[1]for an int array is:A) 12 B) 3 C) 4 D) 16
-
Which declares a pointer to a function taking two ints and returning int?
A)
int *f(int, int);B)int (*f)(int, int);C)int f*(int, int);D)(int *)f(int, int);
Show answers
- B: a[3].
- C: a wild pointer.
- B:
p = &y. - B: 3 elements.
- B:
int (*f)(int, int);(A declares a function returning int *).
Quick Revision for Exams and Interviews
Read this before your interview. The highlighted words are the answer.
Must-remember facts
| & / * | Address of / value at |
|---|---|
| p + n | Moves n × sizeof(type) bytes |
| p2 − p1 | Number of elements |
| Pointer size | Depends on system (8 bytes on 64-bit) |
| const int *p | Value read-only |
| int *const p | Pointer fixed |
| Bugs | NULL, wild, dangling, leak |
Frequently asked questions
- What is the difference between an array and a pointer?
- An array is a block of elements; a pointer stores an address. The array name decays to a pointer, but sizeof and assignment behave differently.
- What is a dangling pointer?
- A pointer to memory that is no longer valid: after free or after a local variable goes out of scope.
- Why use a double pointer?
- To let a function change the caller's pointer, and for 2D dynamic arrays and argv.
Remember
- Pointer = address on paper; * = go and open the door.
- Draw memory for every pointer question.
Next Topic
Next lesson: Predict the Output: C Questions. You will predict the output of tricky C programs, a favourite of written tests.