Search code examples
objective-ccstructcgrectcgrectmake

CGRectMake alternative


I have seen this alternative to using CGRectMake() in order to initialise a CGRect variable:

CGRect frame = (CGRect){0,0,10,10};

My question is, how does CGRect frame = (CGRect){0,0,10,10}; work? What's going on behind the scenes? It looks like a c-style array is being initialised ({x,y,w,h}) which is then being cast as a CGRect struct - is this correct? If so, how is it possible to cast a c style array as a struct?

N.B. I am not asking if it is appropriate to use the above alternative to CGRectMake(), I only wish to understand why/how it works.


Solution

  • It's a so-called compound literal. You can read more about them in this article by Mike Ash: Friday Q&A 2011-02-18: Compound Literals.