Pass by Value Interview Questions for Embedded C: Quiz and Code Challenges
57 questions: multiple choice, write-the-prototype blanks, a parameter-style table, true/false, 6 code challenges with real output and 12 detailed interview answers. Treat it as a model for pass by value interview questions.
57
Questions
Beginner → Advanced
Level
Live score
Auto-graded
Live Score Tracker
0 Correct
0 Wrong
0 Attempted
Answer the auto-graded questions (MCQ, fill-in, true/false) to see your score.
Section 1: Multiple Choice Questions — Pass by Value and by Pointer
These pass by value interview questions appear in nearly every embedded C screening round. Click an answer; explanations always appear.
1. How does C pass arguments to a function?
Even a pointer is passed by value; the pointer is copied.
2. Which call lets a function change the caller’s int x?
The function writes through the copied address.
3. What does a swap(int a, int b) that swaps its parameters do to the caller?
It swaps its own copies only.
4. An array name used as an argument is passed as:
The array decays to T *.
5. What does sizeof(arr) give inside a function whose parameter is int arr[10]?
The parameter is really int *.
6. Which parameter type promises a function will not modify the buffer?
Pointer to const data.
7. What does int *const p mean?
The pointer is const; the data it points at is not.
8. Why pass a large struct by pointer?
Only four or eight bytes are copied.
9. How do you modify a caller’s pointer inside a function?
A T ** parameter.
10. What does the XOR swap *a ^= *b; *b ^= *a; *a ^= *b; do if a and b point to the same variable?
x ^ x is 0, so aliasing destroys the value.
11. A function writes a result through an int *out parameter. What should it also do?
The return value reports success or failure.
12. Which parameter is correct for a function that fills a caller-owned buffer?
Pass the capacity so the function cannot overrun.
13. What is the effect of the C99 keyword restrict on a pointer parameter?
The compiler may optimise assuming no aliasing.
14. Which type should a function use to read a hardware register that changes by itself?
volatile forces every read.
15. Passing NULL to a function that dereferences the pointer without a check leads to:
Check pointer parameters at public entry points.
Section 2: Write or Evaluate (Fill in the Blanks)
Type the declaration or call for the first three items and a short value for the rest.
1. Write the prototype of a swap function taking two int pointers (parameter names a and b, returning nothing):
void swap(int *a, int *b);
2. Write a prototype for copy that reads a const char pointer src and writes a char pointer dst, returning void:
The destination is non-const, the source is const.
3. Write the call that passes the address of count to inc:
The address-of operator.
4. void f(int x) { x = 99; }. After int a = 1; f(a); the value of a is
The function changed its copy.
5. void g(int *p) { *p = 99; }. After int a = 1; g(&a); the value of a is
Written through the pointer.
6. On a 32-bit Cortex-M4, sizeof(int *) is bytes
A pointer is one 32-bit word.
7. To let a function change a caller’s char *s, the parameter type is char s (write the stars)
Pointer to pointer.
8. Inside void h(int a[8]) the type of a is int (write the stars)
Array parameters adjust to pointers.
Section 3: Which Parameter Style Fits?
Choose the best parameter style for each need, as asked in pass by value interview questions about API design.
Need
Your answer
Result
Read-only access to caller data
const T *T *out parameterT **pointer to structpointer plus sizevolatile T *restrictT *const
Return a second result
const T *T *out parameterT **pointer to structpointer plus sizevolatile T *restrictT *const
Change the caller’s pointer
const T *T *out parameterT **pointer to structpointer plus sizevolatile T *restrictT *const
Avoid copying a big struct
const T *T *out parameterT **pointer to structpointer plus sizevolatile T *restrictT *const
Fill a caller buffer safely
const T *T *out parameterT **pointer to structpointer plus sizevolatile T *restrictT *const
Read a changing hardware register
const T *T *out parameterT **pointer to structpointer plus sizevolatile T *restrictT *const
Promise no overlap between buffers
const T *T *out parameterT **pointer to structpointer plus sizevolatile T *restrictT *const
Keep the pointer fixed
const T *T *out parameterT **pointer to structpointer plus sizevolatile T *restrictT *const
Section 4: True or False
Statements typical of pass by value interview questions.
1. C has true pass by reference.
It simulates it by passing an address by value.
2. A function can modify the caller’s variable if it receives its address.
It writes through the pointer.
3. Assigning to a pointer parameter changes the caller’s pointer.
Only the local copy changes.
4. sizeof on an array parameter returns the array size.
It returns the pointer size.
5. Passing a struct by value copies all its members.
Large structs are passed by pointer for that reason.
6. A const int * parameter may be passed a pointer to a non-const int.
Adding const is always allowed.
7. Passing const int * to an int * parameter compiles without a warning.
It discards the qualifier.
8. Two pointer parameters may refer to the same object.
Unless declared restrict, the function must cope with aliasing.
Section 5: Code Snippet Challenges
Trace each program on paper first; the real output follows. These challenges are the code-reading half of pass by value interview questions.
Challenge 1 — Output?: Value versus pointer
#include <stdio.h>staticvoid bump(int x) { x++; }
staticvoid bump_p(int *p) { (*p)++; }
int main(void)
{
int a = 5;
bump(a);
bump_p(&a);
bump_p(&a);
printf(“%d\n”, a);
return0;
}
What is printed?
Output:
7
The first call changes only a copy; the two pointer calls add 2 to a: 5 + 2 = 7.
Challenge 2 — Output?: Reassigning a pointer parameter
#include <stdio.h>staticvoid set(int *p) { int v = 7; p = &v; }
int main(void)
{
int a = 1;
set(&a);
printf(“%d\n”, a);
return0;
}
What is printed?
Output:
1
The function only re-points its local copy of the pointer (to a local that dies anyway); a stays 1.
Challenge 3 — Output?: sizeof on an array parameter
In main sizeof gives 16 (4 ints of 4 bytes). Inside show the parameter is a pointer, so the comparison is true (1).
Challenge 4 — Output?: Filling a caller array
#include <stdio.h>staticvoid fill(int *a, int n)
{
for (int i = 0; i < n; i++)
a[i] = i * i;
}
int main(void)
{
int sq[5];
fill(sq, 5);
printf(“%d %d %d\n”, sq[0], sq[2], sq[4]);
return0;
}
What is printed?
Output:
0 4 16
The function writes the squares 0, 1, 4, 9, 16 into the caller’s array through the pointer.
Challenge 5 — Find the bug: Writing through a pointer to const
#include <stdio.h>staticvoid zero(constint *p)
{
*p = 0;
}
int main(void)
{
int a = 3;
zero(&a);
printf(“%d\n”, a);
return0;
}
What does GCC report?
Compiler result:constwrite.c: In function ‘zero’:
constwrite.c:4:8: error: assignment of read-only location ‘*p’
4 | *p = 0;
| ^The parameter promises not to modify the data; the compiler rejects the assignment. Remove const only if writing is really intended.
The function advances the caller’s pointer twice, from A to C.
Section 6: Pass by Value Interview Questions for Embedded C (with Detailed Answers)
Pass by value interview questions test pointer parameters, const correctness, aliasing and safe buffer APIs. Click a question for a model answer.
Q1. Does C support pass by reference?Basic+
No. C passes every argument by value: the callee gets a copy. Passing an address gives the effect of pass by reference, because the callee can write through the copied address, but the pointer itself is still a copy. C++ has real references.
Q2. Why does a swap function with int parameters not work?Basic+
swap_iq.c
#include <stdio.h>staticvoid bad(int a, int b) { int t = a; a = b; b = t; }
staticvoid good(int *a, int *b) { int t = *a; *a = *b; *b = t; }
int main(void)
{
int x = 1, y = 2;
bad(x, y);
printf(“%d %d\n”, x, y);
good(&x, &y);
printf(“%d %d\n”, x, y);
return0;
}
Output (verified by compiling and running)
1 2
2 1
The first version swaps copies. The second swaps the originals.
Q3. How is an array passed to a function?Basic+
The array name decays to a pointer to its first element, so no elements are copied and the function can modify the caller’s array. The parameter forms int a[], int a[8] and int *a are identical, and sizeof(a) inside the function is the pointer size. Pass the element count as a separate parameter.
Q4. What is the difference between const int *p, int *const p and const int *const p?Intermediate+
Declaration
Data modifiable?
Pointer reassignable?
const int *p
No
Yes
int *const p
Yes
No
const int *const p
No
No
Read the declaration right to left: p is a pointer to const int. For API design, the first form is the one you use most for read-only inputs.
Q5. How do you return more than one result with pointers?Intermediate+
Pass output pointers and return a status code: int parse(const char *s, int *value) returns 0 on success and writes the value only then. Callers check the status before using the output. Document which outputs may be NULL, and never leave an output uninitialised on the error path without saying so.
Q6. Why should you pass the buffer size with a pointer?Intermediate+
A pointer carries no length. Without a size parameter the function cannot prevent a write past the end of the caller’s array, which on a microcontroller silently corrupts neighbouring variables. Passing (buf, size) lets the function truncate or return an error; this is why safer APIs such as snprintf take a size.
Q7. How can you modify a pointer in the caller?Intermediate+
Pass the address of the pointer. The parameter is a T **; the function updates *cur. Tokenisers and parsers use this pattern.
Q8. When would you pass a struct by value instead of by pointer?Intermediate+
When it is small (up to about two registers), when you want an independent copy that the callee may change freely, or when the immutable result is clearer in code. For anything larger, or for a struct that is updated in place such as a driver handle, use a pointer, usually const for read-only access. On Cortex-M4 a struct of up to four words is still copied into r0–r3 under the AAPCS, but larger structs go on the stack or via a hidden pointer.
Q9. What problem does pointer aliasing cause and how does restrict help?Advanced+
If two pointer parameters may refer to overlapping memory, the compiler must assume a store through one could change what the other reads, so it reloads values and cannot vectorise or reorder. Declaring them restrict promises there is no overlap, so it can keep values in registers. If you break the promise (for example, passing overlapping buffers to memcpy-like code) the behavior is undefined; use memmove for overlapping data.
When a and b point to the same variable, the first step sets it to x ^ x = 0, and every later step keeps it at 0, so the value is lost. A temporary-based swap works for aliased pointers. The XOR swap saves one variable but is rarely faster on modern cores and is a common trick question.
Q11. How should a hardware register be passed to a helper function?Advanced+
Use a pointer to a volatile object, for example void set_bits(volatile uint32_t *reg, uint32_t mask). Without volatile, the compiler may cache the register value, merge accesses or remove them. Keep the helper static inline so the access compiles to the same load-modify-store sequence as a direct write, and remember that read-modify-write is not atomic against interrupts; use BSRR where the hardware provides it.
Q12. What checks should a function perform on pointer parameters?Advanced+
At public API boundaries: NULL checks for required pointers, size checks for buffers, and alignment checks where the hardware demands them (a 32-bit access at an odd address faults on some cores). Inside private helpers the contract can be asserted instead. Decide on a policy, document it in the header, and be consistent: crashing early in development with assert and returning an error code in production are both defensible.
Study Plan: Using Your Score on These Pass by Value Interview Questions
Score
Meaning
Next step
90–100%
Excellent
Continue to Lesson 8.3: scope, linkage and recursion.
70–89%
Good
Redo Examples 1, 4 and 7 of the examples page.
Below 70%
Revise
Re-read the theory sections on output parameters and const, then retake the quiz.
Habit to build: Mark every input pointer const and every register pointer volatile; the compiler then enforces your intent.
Lesson 8.2 complete!
Next up: scope, storage duration, linkage and recursion.
2 Comments