C Nested Structures
Represent a site location inside each reading, then traverse a collection of complete records.
How it works
A structure may contain another structure by value. This is useful when a smaller concept, such as a grid location, has meaning of its own and is reused by several record types. An array of the outer structure stores complete records contiguously. A single index then identifies all fields for one reading, reducing the alignment mistakes common in parallel arrays. Be explicit about coordinate units and reference frames; grouping coordinates cannot fix a mismatch between metres and degrees.
Run it locally
Use a hosted C17 compiler in a separate lab folder. The command assumes a driver named cc; substitute gcc, clang or zig cc as appropriate. C does not currently run in Bhylu’s browser playground. Apache, PHP and MySQL are not needed for these C programs.
cc -std=c17 -Wall -Wextra -Wpedantic -Werror nested-records.c -o lessonOn Windows, use -o lesson.exe and run .\lesson.exe in PowerShell. On Unix-like systems run ./lesson. Math-library linking differs by toolchain; GCC on many Unix systems needs -lm after the source.
Download the complete C source. File-writing labs must run in an empty private directory, away from existing reports.
Example
#include <stdio.h>
struct Cell { int row; int col; };
struct Reading { int id; struct Cell cell; int depth; };
int main(void) {
struct Reading readings[] = {{7, {2, 3}, 18}, {8, {2, 4}, 22}};
size_t count = sizeof readings / sizeof readings[0];
for (size_t i = 0; i < count; ++i) {
printf("%d at %d,%d: %d mm\n", readings[i].id, readings[i].cell.row, readings[i].cell.col, readings[i].depth);
}
return 0;
}
Expected result
7 at 2,3: 18 mm
8 at 2,4: 22 mm
Each initializer supplies one id, one nested location and one depth. Traversal stays within the complete array count. A later schema change should use designated initializers where positional nesting becomes hard to review. Passing one reading by value copies the nested cell too, because the cell is stored directly inside the outer object.
For language and library rules, consult the WG14 C working group and its public C11 committee draft, which describes the core facilities used here; C17 incorporates corrections. Check your compiler and runtime documentation for implementation details.