Note: learning the strict aliasing rule. Please be patient.
Code sample (t935.c):
#include <stdio.h>
int f(int* pi, double* pd)
{
*pi = 13;
*pd = 7E-323;
return *pi;
}
int main(void)
{
union { int i; double d; } u;
printf("%d\n", f(&u.i, &u.d));
return 0;
}
Invocations:
$ gcc t935.c -Wall -Wextra -std=c11 -pedantic && ./a.exe
14
$ gcc t935.c -Wall -Wextra -std=c11 -pedantic -O2 && ./a.exe
13
$ gcc t935.c -Wall -Wextra -std=c11 -pedantic -O2 -fno-strict-aliasing && ./a.exe
14
Question: does passing to a function two pointers pointed to members of the same union violate the strict aliasing rule?
The question is originated from Unions and type-punning.
UPD20210901
what would happen if the union type was defined at global scope?
For "union u
is defined at global scope" (actually file scope) case both gcc and clang show the same results as reported above for gcc.