Pointers Quiz and Interview-C Programming Tutorials For Beginners

Need Help or More Information?

Questions about this free C programming course, embedded C training or career guidance? Get in touch.

PREV_LEC NEXT_LEC

Free C Programming Course · Pointers · Lecture 72

Pointers Quiz and Interview

Recap the pointer exercises from this chapter, run one final lab, then test yourself with multiple-choice questions, output-prediction puzzles and real embedded C interview questions. A free C tutorial for embedded systems.

12
Quiz questions
7
Output puzzles
12
Interview questions

Pointers are the single most tested topic in embedded C interview questions, and the best way to learn them is to predict the output of small programs and then check yourself on a compiler. This last lecture of the pointers chapter in our free C programming course pulls together the output-prediction exercises you have just studied, adds a lab that returns a pointer safely, and finishes with a quiz and interview preparation set for embedded C programming jobs.

What You Will Learn

  • A one-page recap of every pointer rule from the exercise set
  • How to return a pointer safely, into the caller’s array
  • How to reason about output-prediction questions without guessing
  • The questions interviewers ask about pointers, and short model answers

Prerequisites

Work through the earlier lectures in this chapter first, especially dangling pointers, pointer parameters, array decay, multidimensional arrays and arrays of pointers. Have a C compiler ready so you can verify each answer.

Pointer Rules Recap From the Exercise Set

The exercise pages end with an answer key. Rather than repeat it, here is what each answer was really testing, with the modern view of the same idea:

Concept testedThe ruleModern note
Uninitialised pointerIt holds garbage and may point anywhere. Initialise before use.Initialise at declaration, to an object or NULL.
Array name is not assignableAn array name behaves like a constant address. You cannot change it.The compiler reports an error, as in the array-assignment demo earlier.
(*p)++ versus p++The first changes the pointed-to value. The second moves the pointer.Parenthesise or split into two statements for clarity.
(arr+2)[3]It means *(arr + 2 + 3), which is arr[5].Subscript is just pointer arithmetic plus a dereference.
Multiplying or dividing pointersNot allowed. Only add or subtract an integer, subtract two pointers, or compare.The compiler rejects it.
Size of pointer versus arrayA pointer has a fixed size, an array has element count times element size.Pointer size depends on the platform: 2 bytes on old 16-bit compilers, 4 on 32-bit MCUs, 8 on 64-bit PCs.
Address printingPrinted addresses depend on where the program is loaded.Use %p with (void *), or print offsets.

Lab: Return a Pointer Safely

Earlier we saw that returning the address of a local is a bug. There is a safe way to return a pointer: point into storage the caller owns. This function finds the largest reading in an array and returns a pointer to that element, so the caller can read it, find its index, or even modify it in place. Save it as ep_find_max.c:

#include <stdio.h>

static int *find_max(int *base, int n)
{
    int *best = base;

    for (int *p = base + 1; p < base + n; p++)
        if (*p > *best)
            best = p;
    return best;                 /* points into the caller's array: safe */
}

int main(void)
{
    int temps[6] = {31, 29, 35, 33, 28, 34};
    int *hot = find_max(temps, 6);

    printf("max = %d at index %td\n", *hot, hot - temps);
    *hot = 0;
    for (int i = 0; i < 6; i++)
        printf("%d ", temps[i]);
    printf("\n");
    return 0;
}
$ gcc -Wall -Wextra -std=c11 -o ep_find_max ep_find_max.c
$ ./ep_find_max
max = 35 at index 2
31 29 0 33 28 34
Why returning a pointer into the caller’s array is safe
temps lives in main312935332834
find_max returnsaddress of the element holding 35
After returnfind_max frame gonetemps still alive, pointer still valid

The pointer outlives find_max because the object it points at belongs to main. Subtracting temps from hot gives the index, 2, which is element count not bytes.

Quiz Part 1: Multiple Choice

Answer each question first, then open the answer. Assume 4-byte int and a 64-bit system unless stated otherwise.

Question 1

Which declaration makes p a pointer to an array of 5 ints?

  • A. int *p[5];
  • B. int (*p)[5];
  • C. int *(p[5]);
  • D. int p*[5];
Show answer

B. The parentheses bind the star to p first. A and C both declare an array of five pointers. D is not valid C.

Question 2

In void f(int a[8]), what does sizeof a give on a 64-bit system?

  • A. 32
  • B. 8
  • C. 4
  • D. 1
Show answer

B. The parameter is really int *a, and a pointer is 8 bytes there. On a 32-bit microcontroller it would be 4.

Question 3

Which of these creates a dangling pointer?

  • A. int *p = NULL;
  • B. int *p; (never assigned)
  • C. int *p = malloc(4); free(p); then using *p
  • D. static int x; int *p = &x;
Show answer

C. After free, p still holds the old address. B is a wild pointer, A is a NULL pointer, and D is perfectly valid.

Question 4

For int m[4][6];, how many bytes does m + 1 move the address?

  • A. 4
  • B. 6
  • C. 24
  • D. 96
Show answer

C. m decays to a pointer to a row of 6 ints. One row is 6 times 4 equals 24 bytes. D is the size of the whole array.

Question 5

For the same array, *(*(m + 2) + 3) is equivalent to which expression?

  • A. m[3][2]
  • B. m[2][3]
  • C. m[5]
  • D. m[2] + 3
Show answer

B. *(m + 2) is row 2, and *(row + 3) is element 3 of that row.

Question 6

If arr is an int array and p is an int pointer, which statement is invalid?

  • A. p = arr;
  • B. arr = p;
  • C. p++;
  • D. int *q = &arr[1];
Show answer

B. You cannot assign to an array name.

Question 7

Why does void swap(int a, int b) fail to swap the caller’s variables?

  • A. It uses the wrong temporary type
  • B. It receives copies of the values
  • C. Local variables cannot be assigned
  • D. It needs the static keyword
Show answer

B. C passes arguments by value. Take pointers and pass addresses to affect the caller’s variables.

Question 8

A function must advance the caller’s char *cursor. What should the parameter type be?

  • A. char *
  • B. char
  • C. char **
  • D. char []
Show answer

C. To change a pointer you pass the address of the pointer. With a read-only buffer use const char **.

Question 9

What does const char *const names[] promise?

  • A. Only the strings are read-only
  • B. Only the table is read-only
  • C. Both the table entries and the characters are read-only
  • D. Nothing, const is ignored in arrays
Show answer

C. The first const protects the characters, the second protects each pointer slot.

Question 10

Which pointer operation is not allowed?

  • A. p + 2
  • B. p - q for two pointers into one array
  • C. p * 2
  • D. p < q
Show answer

C. Multiplying or dividing a pointer has no meaning in C.

Question 11

Is it safe to return the address of a static local variable?

  • A. Never
  • B. Yes, the object lives for the whole program, but the function is no longer reentrant
  • C. Only in C++
  • D. Only if declared volatile
Show answer

B. The lifetime is fine. The shared storage can be a problem if an ISR and main code both call it.

Question 12

Two int pointers point 12 bytes apart inside one array. What is q - p?

  • A. 12
  • B. 3
  • C. 48
  • D. Undefined
Show answer

B. Pointer subtraction counts elements, not bytes: 12 divided by 4.

Quiz Part 2: Predict the Output

Work out each result on paper, then compile and run to check. Each snippet is a complete block inside main. These have all been run, with -Wall -Wextra, and produce the output shown.

Puzzle 1

int v[5] = {2, 4, 6, 8, 10};
int *p = v + 1;
int a = *p++;
int b = *p;
printf("%d %d %td\n", a, b, p - v);
Show output
4 6 2

*p++ reads 4, then moves p to index 2, where b reads 6.

Puzzle 2

int v[5] = {2, 4, 6, 8, 10};
int *p = v;
(*p)++;
p++;
printf("%d %d\n", v[0], *p);
Show output
3 4

The first statement changes the value 2 into 3. The second moves the pointer to the 4.

Puzzle 3

int m[2][3] = {{1, 2, 3}, {4, 5, 6}};
printf("%d %d %d\n", *(*(m + 1) + 2), *(m[1] + 1), **m);
Show output
6 5 1

Row 1 element 2 is 6, row 1 element 1 is 5, and **m is row 0 element 0.

Puzzle 4

int m[2][3] = {{1, 2, 3}, {4, 5, 6}};
int (*q)[3] = m;
printf("%zu %zu\n", sizeof q, sizeof *q);
Show output on a 64-bit system
8 12

One pointer is 8 bytes. What it points at is one row of three ints, 12 bytes.

Puzzle 5

char s[] = "embed";
char *t = s + 2;
printf("%s %zu %td\n", t, strlen(t), t - s);
Show output
bed 3 2

Printing starts at index 2. Three characters remain, and the offset is 2.

Puzzle 6

int v[6] = {0};
int *a = &v[1], *b = &v[5];
printf("%td %d\n", b - a, b > a);
Show output
4 1

Four elements apart, and b is later in memory, so the comparison is true.

Puzzle 7

int x = 7, y = 9;
int *p = &x;
int **pp = &p;
*pp = &y;
**pp += 1;
printf("%d %d\n", x, y);
Show output
7 10

Writing through pp redirects p to y. The next line then increments y, and x is untouched.

Embedded C Interview Questions on Pointers

Say the answer out loud before opening it. Interviewers care about clear reasoning at least as much as the final answer.

Interview Question 1

What is the difference between an array and a pointer?

Show answer

An array is a block of storage whose name converts to an address in most expressions. A pointer is a variable that holds an address. You can reassign a pointer, not an array, and sizeof differs: whole block versus pointer size.

Interview Question 2

Why does sizeof give a different answer for an array parameter?

Show answer

Because the parameter is adjusted to a pointer. The length is lost, which is why functions take a separate count.

Interview Question 3

Distinguish a NULL, a wild and a dangling pointer.

Show answer

NULL holds a known invalid address. Wild is never initialised. Dangling pointed at a valid object that is now gone, by scope exit or free.

Interview Question 4

What is the difference between int *a[4] and int (*a)[4]?

Show answer

The first is an array of four int pointers. The second is one pointer to an array of four ints. Use the right-left rule to read them.

Interview Question 5

What does a[i] really mean?

Show answer

It is defined as *(a + i). Pointer addition scales by the element size, which is why the same formula works for any type.

Interview Question 6

How do you let a function modify the caller’s pointer?

Show answer

Pass its address, a pointer to pointer. Or return the new pointer value and let the caller assign it.

Interview Question 7

What is the difference between const int *p, int *const p and const int *const p?

Show answer

The first points to read-only data but can be repointed. The second can change the data but cannot be repointed. The third can do neither.

Interview Question 8

Can you do arithmetic on a void *?

Show answer

Standard C does not allow it because the pointee has no size. GCC allows it as an extension, treating the size as 1. Portable code casts to char * or uint8_t * first.

Interview Question 9

Is subtracting pointers into two different arrays valid?

Show answer

No. Subtraction and relational comparison are defined only for pointers into the same array object, or one past its end.

Interview Question 10

Why is a pointer one past the end of an array legal, but one before the start not?

Show answer

The standard guarantees a valid address just past the last element so loops like p < base + n work. No such guarantee exists before the first element, so a loop that decrements below the start is undefined behaviour.

Interview Question 11

How do you safely hand data back from a function?

Show answer

Return by value, fill a caller-provided buffer, return a pointer into the caller’s data, use static storage knowing the reentrancy cost, or allocate and document who frees it. Never return the address of a local.

Interview Question 12

Where are function pointers used in firmware?

Show answer

Interrupt vector tables, callback registration, state-machine handler tables, and driver operation structs. Keep such tables const so they stay in flash.

Common Mistakes Checklist

Check before you shipTypical bug it prevents
Every pointer initialised at declarationWild pointer writes
No address of a local stored anywhere long-livedDangling pointer
Every array parameter paired with a lengthBuffer overrun
const on every read-only pointer parameterAccidental writes
Pointer loops stop at base + nOff-by-one overflow
Warnings enabled and treated as errorsType-mismatch bugs that look like they work

Best Practices Summary

  • Read declarations from the name outward and use typedefs for anything with more than one star.
  • Print offsets and sizes, not raw addresses, when you debug or test.
  • Run host-side tests with AddressSanitizer and undefined-behaviour sanitizer before flashing hardware.
  • Keep ownership of every buffer obvious: who allocates, who frees, how long it lives.
  • Prefer clear code over clever pointer tricks, especially in safety-critical firmware.

Performance and Security Considerations

Pointer-based code is fast because it avoids copies, but every pointer is also a way to read or write the wrong place. Validate lengths that come from outside the device, keep constant tables in flash, and never trust a pointer that crossed a task or interrupt boundary without checking its lifetime.

Suggested Images

Image ideaALT text
Cheat sheet of pointer declarations and meaningsC pointers cheat sheet for a free embedded C programming course
Student predicting program output on paperFree C tutorial pointer output prediction practice
Interview whiteboard with pointer diagramEmbedded C interview questions on pointers and arrays

Summary and Key Takeaways

Remember

  • Pointers are addresses with a type. The type decides the step size.
  • Parameters are always copies, so pass an address to change something, and an address of an address to change a pointer.
  • Array parameters are pointers and lose their length.
  • Know the three bad pointers: NULL, wild and dangling.
  • Return pointers only into storage that outlives the function.

Conclusion

That completes the pointer exercises. If you got most of the puzzles right and could explain why, you are ready for the questions embedded teams actually ask. If some surprised you, do not skip them. Compile each one, print sizes and offsets, and change one thing at a time until the behaviour is predictable. The next chapters of this free embedded C course build on exactly these skills.

FAQ: Pointer Practice and Interview Prep

How should I prepare for pointer questions in an embedded C interview?

Practise predicting output by hand, then run the code. Be able to draw memory for arrays, pointers and pointers to pointers, and explain each answer in one or two sentences.

Why do some answers depend on the platform?

Sizes of int and pointers differ between compilers and targets. Good answers state the assumption, for example “on a 32-bit MCU, a pointer is 4 bytes”.

Are these quizzes enough to master pointers?

They are a start. Pair them with real code on a board or simulator: parse a UART frame, drive a ring buffer, build a dispatch table.

What tools help me catch pointer bugs?

Compiler warnings, AddressSanitizer, UBSan, cppcheck and clang-tidy on the host, plus a debugger with watchpoints on target.

Is this course really free?

Yes. Every lecture in this free embedded C course, along with the Linux and embedded systems courses on the site, is free for students.

Finished the Pointers Chapter?

Move on to the next chapter of our free C programming course, or explore the free embedded Linux and device drivers courses.

PREV_LEC NEXT_LEC

Need Help or More Information?

Questions about this free C programming course, embedded C training or career guidance? Get in touch.

1 Comment

Leave a Reply

Your email address will not be published. Required fields are marked *