tags:

views:

2222

answers:

2

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?

+5  A: 
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;
}
Parappa
how do I construct the array in the implementation file?
rob g
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.
Parappa
it compiles, but if i try to access the array from a different function to where it was defined, the compiler throws an error :/
rob g
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.
tvanfosson
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.
Parappa
how would i do that?
rob g
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.
Johannes Schaub - litb
@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.
Adam Rosenfield
+3  A: 

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[] = {...};
Adam Rosenfield
compiles, but when I'm in a different function in the *.m file, the compiler complains that it's undefined.
rob g
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.
Barry Wark
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
Adam Rosenfield