PDA

View Full Version : delcaring 2d array in Objective C




MasterObiWan
Feb 3, 2009, 05:29 AM
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:



lee1210
Feb 3, 2009, 06:31 AM
what's the type? This is not an Xcode isuue, it is a C syntax issue.

-Lee

detz
Feb 3, 2009, 06:40 AM
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:@"55","60","65",nil],
[NSArray arrayWithObjects:@"95","90","85",nil],nil];

MasterObiWan
Feb 3, 2009, 07:05 AM
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}
}



}

MacRumors Guy
Feb 3, 2009, 07:26 AM
someArray[1][0] = someArray[1][1] = someArray[0][1] = someArray[0][0] = 0;

lazydog
Feb 3, 2009, 08:24 AM
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.



#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
Feb 3, 2009, 04:32 PM
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.



#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.