struct AAA
{
char a_1;
int a_2;
};
struct BBB
{
char b_1;
int b_2;
};
int main(void)
{
struct AAA a1 = {2, 4};
struct BBB b1;
b1 = (struct BBB)a1;
return 0;
}
as shown above, “b1 = (struct BBB)a1;” made the complie say “error: conversion to non-scalar type requested”.
the struct AAA and the struct BBB have the same type of members, why does this force conversion fail?
thank you
In the C standard (looking at N1256 as it is freely available)
6.5.4 defines Cast operators.
6.5.4.2 lists as a restriction on cast operators:
6.2.5.21 describes scalar and aggregate types as:
A structure type is therefore definitively NOT a scalar type, which means the constraint on the cast operator is not met. Thus, the code fails.