Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to declare a C-array in header file?

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.

like image 359
Arbitur Avatar asked Nov 19 '25 11:11

Arbitur


1 Answers

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.

like image 192
Sergey Kalinichenko Avatar answered Nov 21 '25 01:11

Sergey Kalinichenko



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!