🚀 Learn BLE on Linux (BlueZ) – Programming Part 1
Hands-on Bluetooth Low Energy course. Special price: ₹399
Enroll Now on Udemy →Array of Structures in Functions
Learn how an array of structures in C travels into a function, why the callee edits the caller’s data, and how embedded C programming uses this pattern for sensor and register tables.
An array of structures in C is the natural way to describe a table of similar things: ten sensors, sixteen GPIO pin configurations, a list of calibration entries. Sooner or later you must hand that table to a function. This lecture of our free C programming course explains exactly what the compiler passes, why changes made inside the function are visible back in main(), and how to write such functions safely. If you are looking for a free C tutorial that treats embedded C programming seriously, you are in the right place.
What You Will Learn
- What actually gets copied when you pass an array of structures in C to a function
- Why
struct sensor list[]andstruct sensor *listmean the same thing in a parameter list - How to modify table entries in place, and how to protect them with
const - Why
sizeoffails inside the callee and what to pass instead - How this pattern appears in real firmware: lookup tables, driver configuration and calibration data
Prerequisites
You should already know how to declare a structure, create an array of structures, and use the dot operator. Basic pointer syntax helps; if pointers still feel shaky, revisit the Pointers chapter of this free embedded C course first.
Why an Array of Structures in C Matters in Firmware
Firmware rarely deals with one object. A thermostat reads several temperature channels, a motor controller manages several phases, and a bootloader walks a table of partitions. Each row has the same fields, so a structure describes one row and an array describes the whole table. Functions then operate on the table: initialise it, calibrate it, print it, search it. Understanding how that table crosses the function boundary is the difference between code that works and code that silently edits a throw-away copy.
| What you pass | What the callee receives | Can the callee change the original? |
|---|---|---|
One element, bank[1] | A full copy of that structure | No, only its own copy changes |
One element’s address, &bank[1] | A pointer to that element | Yes |
The array name, bank | A pointer to bank[0] | Yes, every element |
The array name into a const parameter | A read-only pointer to bank[0] | No, the compiler rejects writes |
How the Array Parameter Really Works
When an array name appears in an expression, it converts to the address of its first element. That conversion is called array-to-pointer decay. It also applies to parameter declarations: the compiler quietly rewrites struct sensor list[] into struct sensor *list. Only the address of element zero is copied into the function. The elements themselves are not copied, which is why the callee works on the caller’s real data.
Pass an Array of Structures in C: Working Example
The program below keeps a bank of three sensors. One function subtracts a fixed offset from every raw reading, another prints the table. Notice that the printing helper receives one element by value, while the table functions receive the base pointer plus an element count.
#include <stdio.h>
#include <stddef.h>
struct sensor {
char tag[12];
int id;
int raw;
};
/* by value: the function works on a private copy */
static void show_one(struct sensor s)
{
printf("%-9s id=%d raw=%d\n", s.tag, s.id, s.raw);
}
/* the array parameter is really a pointer to the first element */
static void apply_offset(struct sensor list[], size_t n, int offset)
{
const struct sensor *p = list;
printf("inside : sizeof(pointer) = %zu\n", sizeof p);
for (size_t i = 0; i < n; i++)
list[i].raw -= offset;
}
static void show_all(const struct sensor *list, size_t n)
{
for (size_t i = 0; i < n; i++)
show_one(list[i]);
}
int main(void)
{
struct sensor bank[3] = {
{ "temp_in", 1, 512 },
{ "temp_out", 2, 498 },
{ "pressure", 3, 1010 }
};
printf("main : sizeof(bank) = %zu, sizeof(bank[0]) = %zu\n",
sizeof bank, sizeof bank[0]);
apply_offset(bank, 3, 12);
show_all(bank, 3);
return 0;
}
Build and Run
$ gcc -std=c17 -Wall -Wextra -o ex21 ex21.c
$ ./ex21
main : sizeof(bank) = 60, sizeof(bank[0]) = 20
inside : sizeof(pointer) = 8
temp_in id=1 raw=500
temp_out id=2 raw=486
pressure id=3 raw=998
Reading the Output
Three details are worth studying. First, sizeof(bank) is 60 inside main() (three elements of 20 bytes each) but sizeof of the parameter is only 8 on a 64-bit machine, because inside the function it is just a pointer. The size information is gone, so the element count must travel as a separate argument. Second, all three raw values dropped by 12, which proves the loop modified the caller’s elements. Third, show_one() received copies, so even if it assigned to s.raw, nothing outside would change.
Protecting the Table With const
A function that only reads the table should say so. Declaring the parameter const struct sensor *list makes any accidental assignment such as list[i].raw = 0; a compile error. In larger firmware projects this is one of the cheapest bug filters available: reviewers instantly see which functions can change the table and which cannot.
Real-World Use Cases
- Calibration tables: apply an offset or gain to every channel after boot
- Pin configuration: an array of pin descriptors handed to an
init_pins()function - Task or timer tables: a scheduler walks an array of entries on every tick
- Protocol descriptors: a parser looks up a command in an array of command structures
Pointer Arithmetic View of the Same Table
Because the parameter is a pointer, the expression list[i] is defined as *(list + i). Adding i moves the address forward by i whole structures, not by i bytes, so the compiler multiplies by the structure size for you. Writing (list + i)->raw reaches the same member as list[i].raw. Knowing this makes it easier to read driver code that walks an array of structures in C with a moving pointer instead of an index, a style you will meet often in embedded C programming.
Common Mistakes With an Array of Structures in C
Using sizeof on the parameter. sizeof(list)/sizeof(list[0]) inside the callee gives pointer size divided by element size, usually 0 or 2, never the real count. GCC warns about this with -Wsizeof-array-argument. Compute the count in the caller and pass it.
- Forgetting the count: without
n, the function cannot know where the table ends - Off-by-one loops: use
i < n, neveri <= n - Expecting by-value semantics: if the callee edits
list[i], the caller’s data changes whether you wanted it or not - Passing the wrong thing:
&bankhas type pointer-to-whole-array, not pointer-to-element; passbank
Best Practices for an Array of Structures in C
Rules to adopt
- Always pass the base pointer together with an element count of type
size_t - Mark read-only parameters
const - Pass large structures by pointer; copying a 20-byte struct is cheap, copying a 200-byte one inside a tight interrupt path is not
- Validate the count before indexing when it comes from outside the program, such as a received packet; an unchecked count is a classic buffer-overflow source
Summary: Array of Structures in C Key Takeaways
- An array parameter is a pointer; only the first element’s address is copied
- The callee modifies the caller’s array of structures in C unless you declare it
const - A single element passed by value is copied completely and cannot affect the original
- Pass the element count explicitly because
sizeofcannot recover it
You now know how a table of records crosses a function boundary. In the next lecture of this free embedded C course we add derived fields to each record and compute them in a loop.
Frequently Asked Questions
Is an array of structures passed by value or by reference in C?
C always passes by value, but the value of an array name is the address of its first element. So the function sees the original elements, which behaves like pass by reference.
Why does sizeof give the wrong size inside my function?
Inside the function the parameter is a pointer, so sizeof returns the pointer size. Pass the element count as an extra argument.
Can I pass a single structure element by value?
Yes. The whole structure is copied, which is safe but costs time and stack for big structures. Use a pointer when the structure is large.
Do I need to write the array size in the parameter, like list[3]?
No. The number is ignored by the compiler. Write list[] or *list and pass the count separately.
How do I stop a function from changing my table?
Declare the parameter as const struct sensor *list. Any write through it then fails to compile.
Is this useful for embedded C programming interviews?
Very. Questions about array decay, sizeof on parameters and const correctness appear in most firmware interviews.
Keep Going With the Free Embedded C Course
Continue the free C programming course on EmbeddedPathashala and build real embedded C programming skills, one short lecture at a time.

2 Comments