Title: How can I define an array in my Objective-C header file?
Tags: c;objective-c
Question: I have this code in my main file:

```int grid[] = { 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 ,
               1 , 2 , 3 , 2 , 3 , 2 , 3 , 1 , 
               1 , 0 , 0 , 0 , 0 , 0 , 0 , 1 , 
               1 , 0 , 1 , 0 , 1 , 0 , 1 , 1 , 
               1 , 0 , 1 , 0 , 0 , 0 , 0 , 1 , 
               1 , 0 , 0 , 0 , 0 , 0 , 1 , 1 , 
               1 , 1 , 1 , 1 , 1 , 1 , 1 , 0 };
```

How do I define it in my header so that I can access the variable throughout my class?



Here is another answer: You can't define it in your header.  You have to declare it in your header and define it in a source (```.m```) file:

```// In MyClass.h
extern int grid[];

// In MyClass.m
int grid[] = {...};
```

Comment for this answer: compiles, but when I'm in a different function in the *.m file, the compiler complains that it's undefined.
Comment for this answer: Any header file which needs to use grid[] needs to have an "extern int grid[]" declaration in it, or needs to #include another header file which has the extern declaration.  See also http://stackoverflow.com/questions/309801/declarations-definitions-initializations-in-c-c-c-java-and-python
Comment for this answer: you want to define it at the "top level" of the .m file (ie. outside of all functions). By convention, global variables such as this are defined at the top of the .m file, just below any #import or #include statements.

Here is another answer: ```extern int grid[];
```

Let's suppose you had some code like this:

```int grid[] = { 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 ,
        1 , 2 , 3 , 2 , 3 , 2 , 3 , 1 , 
        1 , 0 , 0 , 0 , 0 , 0 , 0 , 1 , 
        1 , 0 , 1 , 0 , 1 , 0 , 1 , 1 , 
        1 , 0 , 1 , 0 , 0 , 0 , 0 , 1 , 
        1 , 0 , 0 , 0 , 0 , 0 , 1 , 1 , 
        1 , 1 , 1 , 1 , 1 , 1 , 1 , 0 };

int arr_sum(int* arr, int len)
{
    int sum = 0;
    for (int i = 0; i < len; i++) {
            sum += arr[i];
    }
    return sum;
}

int main(int argc, char** argv)
{
    printf("%d\n", arr_sum(grid, sizeof(grid)/sizeof(int) ));
    return 0;
}
```

If you wanted to separate this out into two different files, say, you could have the following, for example:

in grid.c:

```int grid[] = { 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 ,
        1 , 2 , 3 , 2 , 3 , 2 , 3 , 1 , 
        1 , 0 , 0 , 0 , 0 , 0 , 0 , 1 , 
        1 , 0 , 1 , 0 , 1 , 0 , 1 , 1 , 
        1 , 0 , 1 , 0 , 0 , 0 , 0 , 1 , 
        1 , 0 , 0 , 0 , 0 , 0 , 1 , 1 , 
        1 , 1 , 1 , 1 , 1 , 1 , 1 , 0 };
```

In main.c:

```extern grid[];

int arr_sum(int* arr, int len)
{
    int sum = 0;
    for (int i = 0; i < len; i++) {
            sum += arr[i];
    }
    return sum;
}

int main(int argc, char** argv)
{
    printf("%d\n", arr_sum(grid, sizeof(grid)/sizeof(int) ));
    return 0;
}
```

Comment for this answer: i think with "construct" he meant how he defines the array. but then again this is about objective-c . i've got no clue about that. but looks like it is similar to normal C.
Comment for this answer: how do I construct the array in the implementation file?
Comment for this answer: it compiles, but if i try to access the array from a different function to where it was defined, the compiler throws an error :/
Comment for this answer: You'll need the extern in a header file called "externals.h" -- or something like that.  It will hold all of your external variable declarations.  In your source file containing main, *don't* include the externals.h file, but do include the actual definitions of those external variables.
Comment for this answer: You shouldn't need to construct it anywhere else.  Have the code that you listed above in main.c, and put "extern int grid[]" in a common header file used by the other source files that use grid.  Or if you don't want the header, just put the extern wherever grid is used.
Comment for this answer: Is grid defined within the scope of a function?  You have to move it to a more global scope if you want to use it in other functions.
Comment for this answer: @litb: Objective-C is a *strict* superset of C.  In this case, the question has absolutely nothing to do with any part of the new features of Objective-C, it's a pure C question.

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 2, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 2.5', 'CreationDate': '2009:00:03/17/09', 'FavoriteCount': 2.0, 'PostTypeId': 1, 'Tags': '<c><objective-c>', 'Title': 'How can I define an array in my Objective-C header file?', 'ViewCount': 8763, 'id': 656046, 'user_AboutMe': None, 'user_AccountId': 560537.0, 'user_DisplayName': 'rob g', 'user_Reputation': 113.0, 'user_Views': 26.0, 'user_id': 78327.0}}