I was trying to implement my own variadic function using this code. Instead I got UB.
#include <stdio.h>
void test(int a, ...)
{
char* arg_a = (char*)&a;
char* arg_b = arg_a + sizeof(int);
printf("%c", *arg_b);
}
int main(){
test(1, 'a');
}
So why does this program not print the letter a? Is it not expected that argument 1 ( of test()) will be written in the function stack frame in a low address ex: 0000 0004 (since 0000 0000 will be reserved for return address) then followed by arg_a in the higher address following first arg?
I guess this result is because of something of compiler optimization, or is there is something else?