Is passing a cast pointer by value valid in C? If not why is this working?
#include <stdio.h>
typedef struct
{
int size;
char* str;
} MY_STRUCT;
void print_my_struct_prt(MY_STRUCT* mstrct)
{
printf("%s%d%s%s\n", "size: ", mstrct->size, " content: ", mstrct->str);
}
int main()
{
MY_STRUCT my_ptr_strct;
my_ptr_strct.size = 3;
my_ptr_strct.str = "first test";
print_my_struct_prt(&my_ptr_strct);
print_my_struct_prt(&(MY_STRUCT) { 6, "second test"});
return 0;
}
output:
size: 3 content: first test
size: 6 content: second test
In the first test a stuct is created, the values are set, and the address is passed to print_my_struct_prt().
In the second test I can only guess the values 6 and "second test" are stored in memory and the address to the location in memory is passed to print_my_struct_prt(). Is this correct?
Aside from obvious readability issues, is this acceptable in C?