I tried to assign a struct variable values from an array of the same type of structs using = operator, like below:
struct s {
int dummy;
char somechar[8];
} array_of_s[10];
void f(void)
{
struct s a = array_of_s[0]; // this line gives me error message
}
int main(void)
{
f();
return 0;
}
I'm getting an error message and it says:
a value of type "struct s" cannot be used to initialize an entity of type "struct s"C/C++(144)
But if I hand over array as an argument like below it works like a charm:
struct s {
int dummy;
char somechar[8];
} array_of_s[10];
void f(struct s array_as_parameter[])
{
struct s a = array_as_parameter[0];
}
int main(void)
{
f(array_of_s);
return 0;
}
Why is the first code illegal? Shouldn't I be able to access to external array variable and assign it?