C Aliasing and _Generic
Use a restrict-qualified copy contract and _Generic selection without reading storage through an incompatible type.
How it works
Aliasing means two expressions can refer to the same storage. restrict is a promise about accesses through a pointer during a block execution; breaking it can cause undefined behaviour even if a simple build appears to work. Do not add restrict as decoration to a public function that permits overlapping ranges. _Generic selects an expression according to a controlling expression’s type. It can support a small diagnostic macro, but it does not create runtime reflection or validate an object representation.
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 aliasing-generic.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>
#include <stddef.h>
#define TYPE_LABEL(value) _Generic((value), int: "int", double: "double", default: "other")
/* Requires non-overlapping readable and writable ranges. */
static void copy_readings(int *restrict out, const int *restrict in, size_t count) {
for (size_t i = 0; i < count; ++i) { out[i] = in[i]; }
}
int main(void) {
int input[] = {2, 4, 6};
int output[3];
copy_readings(output, input, 3);
printf("Copied: %d; type: %s\n", output[2], TYPE_LABEL(output[2]));
return 0;
}
Expected result
Copied: 6; type: int
The input and output arrays are distinct, satisfying this simple non-overlap contract. The generic selection names the int expression’s type; its controlling expression is not evaluated. Byte inspection through unsigned char is permitted, but reinterpreting arbitrary bytes as an incompatible int object can violate effective-type and alignment rules. Use explicit encoders or memcpy into a suitable existing object when appropriate.
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.