So, arrays are pointers to their first element.
float color[4]={rColor, gColor, bColor, alpha};
Thus, just plain color points to &color[0];
Now suppose I have a struct thusly:
struct Colors{
float color[4];
};
Now I have found I can do this quite fine:
Colors myColor={{*color}};
I could also just do this:
Colors myColor={{color[0]}};
I am only pointing at one element, but the struct expects 4, so it keeps looking past this element.
First, I want to check that is fine to do, legal and okay. Obviously if you are passing a large array, this is quite convenient on the syntax and typing.
Second, I want to verify the reasoning about why this works. Since color alone is a pointer, the * is an indirection operator that retrieves the memory pointed to, thus, the array. So essentially we get the entire array by just calling its pointer with indirection. Correct?
Intializers of structures, unions and arrays have the particular property that elements that are omitted from it are initialized with
0.To initialize a whole structure as your
Colorsyou can also use any other object of that same type.