Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9239805
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T08:04:25+00:00 2026-06-18T08:04:25+00:00

I saw the syntax below in some example code and am not sure I

  • 0

I saw the syntax below in some example code and am not sure I understand it.

 CGRect imageRect = (CGRect){.size = baseImage.size};

Is this simply a shorthand way of initializing a CGRect equivalent to:

 CGRect imageRect = CGRectMake(0,0,baseImage.size.width, baseImage.size.height);

Is there any benefit to this syntax aside from slightly less typing?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T08:04:26+00:00Added an answer on June 18, 2026 at 8:04 am

    That’s C99 initializer syntax. You can use it with any structure.

    The main advantage to an Objective-C is that it gives you some very Objective-C like syntax, where the fields are close to the values rather than implied by positioning. (That’s not to say this is intentionally similar, or that it’s the only advantage. But it is nice.)

    It’s sometimes slightly more typing, but I use it everywhere now.

    Consider:

    CGRect a = CGRectMake(a+c/2, b+d/2, c, d);
    

    In order to understand this, you need to understand the order of the parameters. You also need to be able to catch the commas easily with your eyes. In this case, that’s pretty easy, but if the expressions were more complicated you’d probably be storing them in a temporary variable first.

    The C99 way:

    CGRect a = (CGRect){
        .origin.x = a+c/2,
        .origin.y = b+d/2,
        .size.width = c,
        .size.height = d
    };
    

    It’s longer, but it’s more explicit. It’s also very easy to follow what is assigned to what, no matter how long the expression are. It’s also more like an Objective-C method. After all, if CGRect was a class, it would probably look like this:

    CGRect *a = [[CGRect alloc] initWithOriginX:x originY:y width:w height:h];
    

    You can also do things like this:

    CGRect a = (CGRect){
        .origin = myOrigin,
        .size = computedSize
    };
    

    Here, you’re building a rectangle using a CGPoint and CGSize. The compiler understands that .origin expects a CGPoint, and .size expects a CGSize. You’ve provided that. All’s gravy.

    The equivalent code would be CGRectMake(myOrigin.x, myOrigin.y, size.width, size.height). By using CGRectMake you’re no longer expressing the same kind of meaning to the compiler. It can’t stop you from assigning part of the size to the origin. It also won’t stop you from assigning the width to the height. It doesn’t even give you a good clue about which is the X and Y; if you’ve used APIs that provide vertical coordinates first, you’ll get it wrong.

    You can assign part from a structure and part from floats as well:

    CGRect a = (CGRect){
        .origin = myOrigin,
        .size.width = c,
        .size.height = d
    };
    

    The CGRectMake function predates C99. I have no evidence to this effect, but I think if C99 had come first CGRectMake probably wouldn’t exist at all; it’s the sort of crusty function you write when your language has no direct way to perform the initialization. But now it does.

    Basically, if you use it for a while, you’ll probably come to prefer C99 syntax. It’s more explicit, more flexible, more Objective-C-like and harder to screw up.

    Unfortunately, as of 4.6 Xcode will not autocomplete structure field names when in the C99 field initializer list.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm not really a guru in JavaScript. I saw this syntax : a !=
I saw an interesting syntax in symfony 2.x and i am not sure how
I just saw some code in bash that I didn't quite understand. Being the
I saw a piece of Scala code with manifest syntax as follows. I do
I saw some websites that (for example): if you want to view your message
I saw some code when studying the open source project: here . But I
I saw some html that displays the results of a PHP code just by
I saw this post on SO which contains C code to get the latest
I recently saw something interesting in some c++ code: try { //doStuff } catch
I saw this syntax: @helper IncludeJS(string url) { <script src=@url type=text/javascript></script> } Placed on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.