What is an array in C? Contiguous memory and its relationship to pointers
Atlasbot · 16/8/2026 · 10 min read
The buffer receives UART data, a lookup table for available sine values, a list of recent sensor samples — most of which use the same basic data structure: array. In C, arrays are much simpler than in other languages, but that simplicity is something that needs to be understood carefully.
Key concepts
An array in C is a sequence of elements of the same data type, allocated consecutively in memory — with no space between elements.
uint16_t adc_samples[5] = {512, 520, 515, 518, 522};
If adc_samples starts at address 0x2000 and each uint16_t takes up 2 bytes, then adc_samples[2] is at address 0x2000 + 2*2 = 0x2004. This is not a detail that needs to be remembered for using arrays — it is exactly how C calculates the index, and is why the arr[i] operation is essentially equivalent to the *(arr + i) pointer operation.
The "degeneracy" array becomes a pointer when passed to the function
When passing an array to a function, C does not copy the entire array — it only passes the address of the first element, the "degeneracy" (decay) array into a pointer. Important consequence: the function receiving array does not know its size, so must pass a separate parameter:
void print_samples(uint16_t *samples, size_t count) {
for (size_t i = 0; i < count; i++) {
printf("%d\n", samples[i]);
}
}
print_samples(adc_samples, 5); // must include the number of elements
In short: Arrays in C do not "know" their own size when passed to another function — they always have to pass along the length, unlike dynamic arrays (
std::vector, Python list) in other languages that store their sizes themselves.
How it works
Address: 0x2000 0x2002 0x2004 0x2006 0x2008
┌────────┬────────┬────────┬────────┬────────┐
Value: │ 512 │ 520 │ 515 │ 518 │ 522 │
└────────┴────────┴────────┴────────┴────────┘
Index: adc_samples[0] [1] [2] [3] [4]
Because the size of the array is fixed at compile time (uint16_t adc_samples[5] is always 5 elements throughout the program life cycle), static arrays of this type are almost always given priority in embedded code instead of dynamic allocation (malloc) — completely avoiding the risk of memory fragmentation (heap fragmentation) when the program has to run stably continuously for months on a robot, with no operating system to clean up the garbage. help.
Technical details
Formulas
The address of an element is calculated according to the formula stated in the Main Concepts section; There is also a practical formula to count the number of elements of a static array while it is still an array (not degenerated into a pointer):
Element address i = Original address + (i × sizeof(data type))
Number of elements = sizeof(array) / sizeof(array[0])
VD: uint16_t adc_samples[5]
sizeof(adc_samples) = 10 byte (5 × 2 byte)
sizeof(adc_samples[0]) = 2 byte
Number of elements = 10 / 2 = 5
Parameters
Common static array buffer size in embedded code, choose based on experience to balance between enough usage and saving RAM:
| Buffer type | Typical size |
|---|---|
| UART receiving buffer (ring buffer) | 64 – 256 byte |
| ADC sample buffer (moving average filter) | 8 – 32 samples |
| Lookup table (eg: sine value) | Depending on the resolution needed, usually 90-360 elements |
Register / Bit / Byte
On microcontrollers with a DMA (Direct Memory Access) controller, a static array is often used as a destination buffer so that the hardware automatically writes data to without the CPU intervening each byte — the starting address of the array is loaded directly into the DMA configuration register (eg, DMA_SxM0AR on STM32, stores the memory address destination), the CPU only needs to read the array again after DMA reports completion via interrupt, instead of having to manually read each byte of UART/ADC data sent.
Practical applications
Ring buffer (ring buffer) — a static array with 2 read/write indexes that cycle automatically when reaching the end of the array — is a standard data structure for receiving UART data by interrupt (discussed in the Interrupt article): ISR only writes newly received bytes to the array and increments the write index, the main program reads the data out at its own speed and increments the read index — completely separate from the data reception speed (determined by the UART hardware determined) from the processing speed (determined by the main program), avoiding data loss when the main program is busy with other things when new bytes arrive.
Practical example
#include <stdio.h>
#include <stdint.h>
void print_samples(uint16_t *samples, size_t count) {
for (size_t i = 0; i < count; i++) {
printf("samples[%zu] = %d\n", i, samples[i]);
}
}
int main() {
uint16_t adc_samples[5] = {512, 520, 515, 518, 522};
printf("So phan tu (tinh bang sizeof) = %zu\n",
sizeof(adc_samples) / sizeof(adc_samples[0]));
print_samples(adc_samples, 5);
return 0;
}
Output:
So phan tu (tinh bang sizeof) = 5
samples[0] = 512
samples[1] = 520
samples[2] = 515
samples[3] = 518
samples[4] = 522
Technique sizeof(arr) / sizeof(arr[0]) is only correct when calculating right on the original array variable (adc_samples in main()) — if calculated inside print_samples() (where the parameter has been degenerate to a pointer), the result will be completely wrong (see Common Errors section).
Common mistakes
- Use
sizeof(arr)/sizeof(arr[0])inside a function that receives an array as parameter — the parameter has been degenerate to a pointer,sizeof(arr)now returns the pointer size (4 or 8 bytes), not the original array size — this technique is only correct when used directly on a real array variable, not passed to any function. - Out-of-bounds array access (out-of-bounds) — C does not self-check valid indexes,
adc_samples[10]on array with only 5 elements still compiles and runs, only reads/writes incorrectly to adjacent memory areas — this error is not reported immediately but often manifests as strange behavior in a completely different place in the code. - Forgot to pass the number of elements when calling the array receiving function — because the array degeneracy into a pointer does not carry size information, the array receiving function always has to receive an additional length parameter, forgetting to pass or passing the wrong number, leading to reading/writing outside the real data area.
- Use
mallocfor buffers that need to last throughout the life of the program — repeated dynamic allocation on a system that runs continuously for months can cause gradual memory fragmentation; With buffers of known and fixed size, declaring a global static array is much safer.
Conclusion
Arrays in C are simply a continuous block of memory — but that simplicity (no boundary checking, no self-saving of size) requires the programmer to carefully manage it, especially when the array degenerates into a pointer when passed to a function. Ring buffers based on static arrays are an almost mandatory paradigm when working with interrupt-control interfaces. The next article in this category is about Functions — how to organize code into reusable blocks, including how to pass arrays/structs learned in the previous 2 articles.
Get new articles and lessons by email
When a new technical article or course module is published, Atlas Robotics sends you a short email. No spam.
Unsubscribe any time with the link in each email. See our Privacy policy.