Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

MasterObiWan

macrumors newbie
Original poster
Jan 12, 2009
18
0
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:
 

detz

macrumors 65816
Jun 29, 2007
1,051
0
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];
 

MasterObiWan

macrumors newbie
Original poster
Jan 12, 2009
18
0
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}
}



}
 

lazydog

macrumors 6502a
Sep 3, 2005
709
6
Cramlington, UK
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}
}



}

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
 

MasterObiWan

macrumors newbie
Original poster
Jan 12, 2009
18
0
someArray[1][0] = someArray[1][1] = someArray[0][1] = someArray[0][0] = 0;

:)

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

it worked, thx.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.