1. Welcome to the new MacRumors forums. See our announcement and read our FAQ

constant c structs

Discussion in 'Mac Programming' started by davbeck, Oct 17, 2009.

  1. macrumors regular

    I tried to make a const NSPoint using NSMakePoint but I got errors that it wasn't a compile time constant. Makes sense that you couldn't do it but how would I do it then?
  2. macrumors 68040


    const NSPoint x = {(CGFloat)1.0,(CGFloat)2.0};
    For initializing a C struct, you list it's elements in a comma separated list in the order of its elements inside curly braces. In this case x will be 1.0, y will be 2.0.

  3. macrumors regular

    Is there any worry that the struct will be rearranged in a future version or is that not something that would happen.
  4. macrumors 68040


    It would be pretty sad if this happened, but it's definitely a risk. I don't believe there's a means to initialize a struct using the named members. I guess in this case as long as you were setting x and y to the same thing you'd always be safe. Otherwise... hm:
    	NSPoint x = NSMakePoint((CGFloat) 2.0, (CGFloat)1.0);
    	const NSPoint y = x;

    EDIT: Looks like there's a C99 feature, so I was wrong about not being able to initialize by named member:
    const NSPoint z = {.x = (CGFloat)2.0, .y = (CGFloat) 1.0};

Share This Page