Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a C-array of CGPoint that I want to declare in the header file .h.

CGPoint checkPoint[8];

But when I try to give it a value in .m:

checkPoint[8] = { //<-- Error Here
    CGPointMake(0, -10),
    CGPointMake(10, 0),
    CGPointMake(0, 10),
    CGPointMake(-10, 0),
    CGPointMake(-10, -10),
    CGPointMake(10, -10),
    CGPointMake(10, 10),
    CGPointMake(-10, 10)
};

It gives me an error pointing at the first opening bracket: Expected expression

Im not very used with C-arrays, how is the correct way of doing this?

EDIT

I have tried with extern in the header file, but I get this error message: Type name does not allow storage class to be specified.

share|improve this question
 
Please show the section of the header where you declare extern CGPoint checkPoint[8]; It must be done outside any type / interface declaration, otherwise extern would not be valid. –  dasblinkenlight Nov 8 at 10:50
 
Oh, I didn't know that it had to be outside of the @interface, I don't get that error anymore, but I still get the error when I try giving it a value. –  Arbitur Nov 8 at 10:55
 
That too needs to be outside any @implementation block in a .m file, and it may not call CGPointMake. –  dasblinkenlight Nov 8 at 10:57
 
xD Im so unused to these types of arrays. Thanks man it works like I want now, I accept your answer and you can add that it needs to be outside of the interface and implementation so other people can find it faster ;) –  Arbitur Nov 8 at 11:04

2 Answers

up vote 3 down vote accepted

You need to add extern to the declaration in the header:

extern CGPoint checkPoint[8];

This would make it a declaration, rather than a declaration/definition. Note that the definition wouldn't compile because of calls to CGPointMake in the initializer (must be compile-time constant, but CGPointMake is a function).

You can replace CGPointMake with {.x= 0, .y=-10} style of initializer, like this:

checkPoint[8] = { //<-- Error Here
    {.x=0,   .y=-10},
    {.x=10,  .y=0},
    {.x=0,   .y=10},
    {.x=-10, .y=0},
    {.x=-10, .y=-10},
    {.x=10,  .y=-10},
    {.x=10,  .y=10},
    {.x=-10, .y=10}
};

Note : (in response to a thread of comments to question)

extern is used for declaring global variables. They do not belong to any class, so their declaration needs to be outside an @interface, and their definition needs to be outside the @implementation block.

share|improve this answer
 
I've tried that already, sorry for not including it in the question, when I use the extern I get an error message: Type name does not allow storage class to be specified. –  Arbitur Nov 8 at 10:46
 
@Arbitur Are you trying to declare the extern variable as an instance variable for a class? There are four or five SO questions relating to that error message and they all relate to putting extern or static on an instance variable declaration. –  JeremyP Nov 8 at 12:20

Declare it as extern CGPoint checkPoint[8] in the header, and then in one source file you can initialise it as CGPoint checkPoint[8] = { { 0.0f, -10.0f }, { 10.0f, 0.0f }, … };

Without the extern you would be defining it anew in every file that includes the header. The initialisers also need to be compile-time constants.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.