C course menu
Intermediate C Interview Questions: Pointers, Memory, Structs and Macros
Intermediate C interview questions with answers and runnable code: pointers and pointer arithmetic, NULL, dangling and wild pointers, malloc vs calloc, memory leaks, arrays vs pointers, struct vs union, static and extern, const pointers, macros vs functions, and function pointers.
On this page
- 1. What is a pointer, and what do & and * do?
- 2. What are NULL, dangling and wild pointers?
- 3. What does pointer arithmetic do?
- 4. Are arrays and pointers the same?
- 5. What is the difference between malloc and calloc?
- 6. What is a memory leak, and how do you avoid it?
- 7. What is the difference between a struct and a union?
- 8. What does static mean in C?
- 9. Explain const int p, int const p and const int *const p.
- 10. What are the drawbacks of macros compared with functions?
- 11. What is a function pointer? Give a use.
- 12. What is the output, and why?
- 13. Find the length of a string without strlen
- Keep going
Once the basics are settled, interviewers move to pointers and memory, the topics that show whether you really understand C. Try each answer yourself before opening the solution.
1. What is a pointer, and what do & and * do?#
Show solution
A pointer is a variable that stores a memory address. &x gives the address of x; *p reaches the value at the address stored in p:
int x = 10;
int *p = &x;
*p = 20;
printf("%d %d\n", x, *p);20 202. What are NULL, dangling and wild pointers?#
Show solution
- NULL pointer: deliberately points to nothing; check for it before dereferencing.
- Wild pointer: an uninitialised pointer holding a random address (
int *p;). - Dangling pointer: points to memory that is no longer valid, after
free, or to a local variable of a function that has returned.
Dereferencing any of them is undefined behavior. Initialise pointers (to NULL if nothing else) and set them to NULL after free.
3. What does pointer arithmetic do?#
Show solution
Adding n to a pointer moves it n elements, not bytes: the step is scaled by the size of the pointed-to type:
int a[] = {10, 20, 30, 40};
int *p = a;
printf("%d %d\n", *(p + 2), (int)(&a[3] - p));
printf("%d\n", (int)((char *)(p + 1) - (char *)p));30 3
4Subtracting two pointers into the same array gives the number of elements between them.
4. Are arrays and pointers the same?#
Show solution
No. An array name decays to a pointer to its first element in most expressions, and a[i] is defined as *(a + i). But an array is a block of storage, while a pointer stores one address:
int a[5] = {0};
int *p = a;
printf("%zu %zu\n", sizeof a, sizeof p);20 8(64-bit system.) Arrays cannot be assigned or re-pointed; pointers can. Inside a function, an array parameter is really a pointer, so sizeof gives the pointer's size there.
5. What is the difference between malloc and calloc?#
Show solution
Both return NULL on failure, and both are released with free. realloc resizes a block; assign its result to a temporary so a failure does not lose the original.
6. What is a memory leak, and how do you avoid it?#
Show solution
A leak is allocated memory that is never freed after the last pointer to it is lost. Long-running programs that leak eventually run out of memory. Avoid it by:
- freeing every allocation on every path, including error paths (a
goto cleanuppattern helps); - making ownership clear: which function is responsible for each
free; - using tools such as Valgrind or AddressSanitizer (
-fsanitize=address).
7. What is the difference between a struct and a union?#
Show solution
In a struct every member has its own storage; in a union all members share the same storage, so only one holds a meaningful value at a time:
#include <stdio.h>
struct S { int i; double d; char c; };
union U { int i; double d; char c; };
int main(void) {
printf("%zu %zu\n", sizeof(struct S), sizeof(union U));
return 0;
}24 8The struct is 24 bytes here, not 13, because of padding for alignment. Unions are used for memory-saving variant types, usually with an enum tag recording which member is active.
8. What does static mean in C?#
Show solution
It has two meanings, depending on where it is used:
- On a local variable: static storage duration. The variable keeps its value between calls and is initialised once.
- On a global variable or function: internal linkage. The name is private to its
.cfile.
extern is the opposite of the second meaning: it declares a variable defined in another file.
9. Explain const int *p, int *const p and const int *const p.#
Show solution
Read right to left:
const int *p: pointer to a constint. You cannot change*p, but you can pointpelsewhere.int *const p: const pointer to anint. You can change*p, but notp.const int *const p: neither can change.
int main(void) {
int a = 1, b = 2;
const int *p = &a;
p = &b; /* fine */
*p = 5; /* error: assignment of read-only location */
return 0;
}10. What are the drawbacks of macros compared with functions?#
Show solution
Macros are text substitution, so:
- they are not type-checked;
- arguments can be evaluated more than once;
- missing parentheses break precedence.
#include <stdio.h>
#define SQUARE(x) x * x
int main(void) {
int i = 3;
printf("%d\n", SQUARE(i + 1)); /* i + 1 * i + 1 */
return 0;
}7Prefer static inline functions, const variables and enums; keep macros for things only the preprocessor can do (include guards, conditional compilation, # and ##).
11. What is a function pointer? Give a use.#
Show solution
A variable that holds a function's address, so the function to call can be chosen at run time. Callbacks such as the comparison function for qsort are the classic use:
#include <stdio.h>
#include <stdlib.h>
int descending(const void *a, const void *b) {
int x = *(const int *)a, y = *(const int *)b;
return (y > x) - (y < x);
}
int main(void) {
int v[] = {3, 9, 1, 7};
qsort(v, 4, sizeof v[0], descending);
printf("%d %d %d %d\n", v[0], v[1], v[2], v[3]);
return 0;
}9 7 3 112. What is the output, and why?#
int i = 5;
printf("%d %d\n", i++, i++);Show solution
It has no single correct answer: modifying i twice in one expression without sequencing is undefined behavior, and the order of argument evaluation is unspecified anyway. The right interview answer is to say so, and to rewrite the code with separate statements. Questions like this test whether you know about undefined behavior, not your guess at what one compiler prints.
13. Find the length of a string without strlen#
Show solution
#include <stdio.h>
size_t my_strlen(const char *s) {
const char *p = s;
while (*p) p++;
return (size_t)(p - s);
}
int main(void) {
printf("%zu %zu\n", my_strlen("pointer"), my_strlen(""));
return 0;
}7 0Keep going#
- Advanced C interview questions: undefined behavior, volatile, alignment, endianness and more.
- C coding challenges.
Finished this lesson?