typedef struct Quiz Interview Prep- Embedded C Programming Course

🚀 Learn BLE on Linux (BlueZ) – Programming Part 1

Hands-on Bluetooth Low Energy course. Special price: ₹399

Enroll Now on Udemy →
💬 Need help or career guidance? Ask us!Contact Us →

typedef struct Quiz Interview Prep

Lecture 51 of our free C programming course: typedef with structures, then a quiz and interview questions covering unions, endianness and typedef.

Chapter 10

Structure and Union

Level

Beginner to Intermediate

Reading time

20 minutes

Writing struct sensor_node every time you declare a variable gets tiring fast. A typedef struct gives the structure a short, clean name, and it is how almost every embedded C driver header is written. This final lecture of the structure and union chapter in our free C programming course shows the three ways to combine typedef with a structure, explains the one case where the tag is mandatory, and then tests everything from lectures 46 to 51 with a quiz and common embedded C interview questions. If you are preparing for firmware interviews, this free embedded C course lecture is for you.

What You Will Learn

  • Three ways to write a typedef struct
  • When a structure tag is required (self-referential structures)
  • How to build and walk a small linked list with a typedef type
  • Common mistakes with typedef and structures
  • A quiz and interview questions on unions, byte order and typedef

Prerequisites

You should know structures, pointers to structures and the typedef basics from lectures 49 and 50.

Three Ways to typedef a Structure

StyleExampleWhen to use
Separate stepsstruct point { int x; int y; }; typedef struct point point_t;You want the tag and the alias defined separately
Combined with a tagtypedef struct node { int v; struct node *next; } node_t;Required when the structure points to itself
Taglesstypedef struct { int x; int y; } point_t;Simple data types that never refer to themselves

In every style, the alias is just another name. After typedef struct sensor_node sensor_node_t;, the types struct sensor_node and sensor_node_t are exactly the same type.

Two names, one structure
tag namestruct sensor_node
aliassensor_node_t
layoutid (1 byte)pos (point_t, 4 bytes)next (pointer)

Why a Self-Referential Structure Needs a Tag

Inside the braces of a structure, the alias name does not exist yet, because the typedef is only completed at the closing semicolon. So a node that stores a pointer to the next node must use the tag name: struct sensor_node *next;. A tagless typedef cannot refer to itself, which is the reason linked lists always carry a tag.

Complete Example: Three Linked Sensor Nodes

/* file: typedef_struct.c */
#include <stdio.h>
#include <stdint.h>

typedef struct {
    int16_t x;
    int16_t y;
} point_t;

typedef struct sensor_node {
    uint8_t  id;
    point_t  pos;
    struct sensor_node *next;
} sensor_node_t;

int main(void)
{
    sensor_node_t n3 = { 3, { 5, -2 }, NULL };
    sensor_node_t n2 = { 2, { 1,  1 }, &n3 };
    sensor_node_t n1 = { 1, { 0,  0 }, &n2 };

    for (const sensor_node_t *cur = &n1; cur != NULL; cur = cur->next)
        printf("node %u at (%d, %d)\n", cur->id, cur->pos.x, cur->pos.y);

    printf("sizeof(point_t)=%zu sizeof(sensor_node_t)=%zu\n",
           sizeof(point_t), sizeof(sensor_node_t));
    return 0;
}
$ gcc -std=c11 -Wall -Wextra -o typedef_struct typedef_struct.c
$ ./typedef_struct
node 1 at (0, 0)
node 2 at (1, 1)
node 3 at (5, -2)
sizeof(point_t)=4 sizeof(sensor_node_t)=16

The 16-byte node size comes from a 1-byte id, 1 byte of padding, 4 bytes for the point (2-byte aligned), more padding so the pointer sits on an 8-byte boundary, then the 8-byte pointer. On a 32-bit microcontroller the same structure would be smaller.

Linked list of sensor_node_t
n1id 1, pos (0,0)nextn2id 2, pos (1,1)nextn3id 3, pos (5,-2)next is NULL

Embedded Use Cases

  • Driver handles: typedef struct uart_dev uart_dev_t; in a public header, with the structure body hidden in the .c file.
  • Message queues and task lists: RTOS control blocks are typedef structs with next pointers.
  • Configuration blocks: one typedef struct passed by pointer to an init function.

Common Mistakes

  • Using the alias name inside its own structure body (compile error). Use struct tag *.
  • Forgetting the semicolon after the closing brace of a typedef struct.
  • Using a tagless typedef for a type that must reference itself.
  • Assuming sizeof of a structure equals the sum of its members; padding changes it.

Best Practices

Guidelines

  • Keep a tag whenever the type may be forward-declared or self-referential.
  • Use the _t suffix, or a project prefix, consistently.
  • Pass large structures by pointer, using const when the function only reads.
  • Print sizeof during bring-up to see padding on each target.

Chapter Summary

  • Unions share storage; arrays of unions are sized by the largest member and need a tag to be safe.
  • Byte order decides how multi-byte values appear in memory; use shifts or standard conversions at wire boundaries.
  • A union overlay can show raw bytes, and structure dumps reveal padding.
  • typedef creates aliases for basic, pointer, array, function and structure types, with no run-time cost.

Quiz: Test Yourself

Try each question first, then open the answer. Assume a 64-bit little-endian PC unless stated.

  1. A union has members uint8_t a; uint32_t b; double c;. What is its sizeof?
    Show answer

    8 bytes, the size of the largest member (double), and 8 is also a suitable alignment.

  2. You write u.b = 5; and then print u.c. Is the output reliable?
    Show answer

    No. Only the member last written holds a meaningful value for its type; the double view is a reinterpretation of the bytes.

  3. How do you reach the field mode of a structure named fields inside a union variable cfg?
    Show answer

    cfg.fields.mode

  4. A little-endian machine stores 0xAABBCCDD as a 32-bit value. Which byte is at the lowest address?
    Show answer

    0xDD, the least significant byte.

  5. What does htonl(0x11223344) return on a big-endian machine?
    Show answer

    The same value, 0x11223344, because host order already equals network order.

  6. Does typedef int marks; create a new type incompatible with int?
    Show answer

    No. It is only an alias; marks and int are the same type.

  7. Which declaration is invalid: typedef int a, b; or typedef static int c;?
    Show answer

    The second. typedef cannot be combined with a storage class such as static.

  8. Given #define PI int * and typedef int *Pt;, how do PI x, y; and Pt x, y; differ?
    Show answer

    With the macro, x is a pointer and y is an int. With the typedef, both are pointers.

  9. Given typedef uint8_t mac_t[6];, what does sizeof report for a mac_t parameter inside a function?
    Show answer

    The pointer size (8 on a 64-bit PC), because array parameters decay to pointers.

  10. Why must a linked-list node use a structure tag even when you add a typedef?
    Show answer

    The typedef name is not yet defined inside the structure body, so the self pointer must be written struct tag *.

Interview Questions

These questions come up regularly in embedded C and firmware interviews. Practise answering them out loud in your own words.

  1. What is the difference between a structure and a union?
    Show answer

    A structure gives each member its own storage, so its size is at least the sum of the members. A union overlays all members at one address, so its size is that of the largest member and only one member is meaningfully active at a time.

  2. Write a function that tells whether the machine is little-endian.
    Show answer

    Store 0x01020304 in a uint32_t, view it through a uint8_t array in a union (or an unsigned char pointer), and test whether the first byte is 0x04. If yes, little-endian.

  3. Why not send a structure directly over a network or serial link?
    Show answer

    The receiver may have different padding, alignment and byte order. Serialise each field explicitly, using shifts and a documented byte order.

  4. How do you make a union safe to use?
    Show answer

    Wrap it in a structure with a tag (an enum) recording which member is active, and update the tag on every write.

  5. typedef or #define for a type, and why?
    Show answer

    typedef. The compiler understands it, scope applies, it handles pointer declarations of several variables correctly and it shows up in debugger and error messages.

  6. Why can a structure be larger than the sum of its members?
    Show answer

    Padding is inserted so each member is aligned to its natural boundary and so arrays of the structure stay aligned.

  7. How do you declare an array of function pointers readably?
    Show answer

    Create a typedef for the function pointer, such as typedef void (*isr_t)(void);, then declare isr_t vectors[16];.

  8. Is reading a different union member than the one written allowed in C?
    Show answer

    Yes since C99, but the result is implementation-defined and can be a trap value. It is not guaranteed in C++, where memcpy is the portable choice.

  9. What is network byte order and which functions convert to it?
    Show answer

    Big-endian. Use htons and htonl to convert from host order, and ntohs and ntohl to convert back.

  10. What problem does a const typedef pointer cause?
    Show answer

    Writing const before a pointer typedef makes the pointer constant, not the data it points to, which is the opposite of what many programmers expect.

Frequently Asked Questions

What is the difference between typedef struct and struct?

Plain struct requires writing the struct keyword on every declaration. A typedef struct lets you use a single short name instead.

Do I need a tag when using typedef struct?

Only when the structure refers to itself or must be forward-declared. Otherwise a tagless typedef is fine.

Can I use the typedef name inside its own structure?

No. The name is not defined until the declaration ends, so use struct tag *.

Does typedef struct change how the structure is stored?

No. It only adds a name; layout and size are unchanged.

Where can I practise more embedded C interview questions?

Continue through the free embedded C course on EmbeddedPathashala, where every chapter ends with a quiz and interview questions.

Is this C programming course really free?

Yes. All lectures, examples and quizzes on EmbeddedPathashala are free to read.

Chapter 10 Complete: Structure and Union

Great work finishing this chapter of our free C programming course. Keep going with the free embedded C course and build real firmware skills.

💬 Need help or career guidance? Ask us!Contact Us →

2 Comments

Leave a Reply

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

×

🚀 BLE on Linux (BlueZ)

Hands-on course. Only ₹399

Enroll Now →