delcaring 2d array in Objective C

Discussion in 'Mac Programming' started by MasterObiWan, Feb 3, 2009.

  1. macrumors newbie

    Joined:
    Jan 12, 2009
    #1
    Hey guys!

    Anyone know how to declare a 2d array in objective C?

    here is how I did it:

    iAmAn2dArray[2][2] = { {0, 1},
    {0,0},
    }

    As usual Xcode gives me errors and the documentation is missing in action.
    I also wish Xcode was more like Eclipse, pressing the build button is frightning sometimes.
    :apple::apple::apple::apple:
     
  2. macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #2
    what's the type? This is not an Xcode isuue, it is a C syntax issue.

    -Lee
     
  3. macrumors 65816

    Joined:
    Jun 29, 2007
    #3
    That's fine if you want it in C, are you writing your stuff in C or Objective-C?

    int iAmAn2dArray[3][3] = { {55, 60, 65},
    {95, 90, 85} };


    If it's objective-C then array's are objects so you have to create them and add them to each other.

    NSArray *iAmAn2dArray = [NSArray arrayWithObjects:
    [NSArray arrayWithObjects:mad:"55","60","65",nil],
    [NSArray arrayWithObjects:mad:"95","90","85",nil],nil];
     
  4. thread starter macrumors newbie

    Joined:
    Jan 12, 2009
    #4
    I am writing it in C.

    Is there a way to declare it as a global variable then add the variables later on in a method?


    example:

    int someArray[2][2];


    (void)method{

    someArray[2][2] = {
    {0, 0}
    {0, 0}
    }



    }
     
  5. macrumors member

    Joined:
    Sep 17, 2008
    #5
    someArray[1][0] = someArray[1][1] = someArray[0][1] = someArray[0][0] = 0;
     
  6. macrumors 6502a

    Joined:
    Sep 3, 2005
    Location:
    Cramlington, UK
    #6
    If you particularly want to use the inline {...} notation then you'll have to use pointers.

    Code:
    
    #include <stdio.h>
    
    typedef int array2x2[2][2] ;
    
    int main( int argc, const char * argv[] )
    {
        int value = 39 ;
        array2x2* test ;
    	
        test = & (array2x2) { { 1, 2 }, { 3, 4 } } ;
        printf("2) %d, %d, %d, %d\n", (*test)[0][0], (*test)[0][1], (*test)[1][0], (*test)[1][1]);
    
        test = & (array2x2) { { 1, 2 }, { 4, 5 } } ;
        printf("2) %d, %d, %d, %d\n", (*test)[0][0], (*test)[0][1], (*test)[1][0], (*test)[1][1]);
    
        test = & (array2x2) { { 4, value }, { value + 34, value + 36 } } ;
        printf("3) %d, %d, %d, %d\n", (*test)[0][0], (*test)[0][1], (*test)[1][0], (*test)[1][1]);
    
        return 0;
    }
    
    b e n
     
  7. thread starter macrumors newbie

    Joined:
    Jan 12, 2009
    #7
    :)

    it worked, thx.
     

Share This Page