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

North Bronson

macrumors 6502
Original poster
Oct 31, 2007
395
1
San José
Does anyone use a lot of C arrays in Objective-C? If anyone could help me out that would be great. I have a handle with NSArrays, but I think a C array would be a little cleaner for what I'm trying to do.

I guess I was looking for something like this:

@interface myObject: NSObject
{
. . .
int matrix[3][5];
. . .
}

-(void) setMatrix: (int) newMatrix
{
matrix = newMatrix
}

I can't figure out how to write the setter. I've having a hard time figuring out how to pass *newMatrix* as an argument over to *setMatrix*.

I guess my question is that if I have some newMatrix that is 3 x 5, how could I write the method to set the matrix in my object to this newMatrix?
 

North Bronson

macrumors 6502
Original poster
Oct 31, 2007
395
1
San José
Okay. Thanks for the help. I kind of wish there was some simpler shortcut than looping through the whole array. It sounds like the NSArray might be simpler after all. I'll probably stick with that.
 

Cromulent

macrumors 604
Oct 2, 2006
6,817
1,102
The Land of Hope and Glory
Okay. Thanks for the help. I kind of wish there was some simpler shortcut than looping through the whole array. It sounds like the NSArray might be simpler after all. I'll probably stick with that.

One does have to ask the obvious question though. Why do you want to copy the contents of one array to another? Seems like a complete waste of processor time.
 

North Bronson

macrumors 6502
Original poster
Oct 31, 2007
395
1
San José
One does have to ask the obvious question though. Why do you want to copy the contents of one array to another? Seems like a complete waste of processor time.

That's a good point. That's also a reason why I thought the NSArray might end up cleaner.

I have a 26 x 3 x 3 array of different strategies my agents can play. The agent picks a strategy and plays this for the whole game. Each strategy is a 3 x 3 array that tells the agent which action to play for each signal (three signals, three actions). Each entry is an integer from {0, 1, 2}.

I guess I thought that rather than have the agents look to this "playbook" with every turn, each agent would remember its own strategy. That's why I was trying to declare the strategy array in the @interface of the Agent object. It might not be a good way to go about things like you said.
 

SydneyDev

macrumors 6502
Sep 15, 2008
346
0
You could also copy the underlying memory of the array:

Code:
#include <string.h>

int matrix[3][5];
matrix[0][0] = 1;
matrix[2][4] = 3;
printf("%d\n", matrix[0][0]);
printf("%d\n", matrix[2][4]);

int matrixcopy[3][5];
memcpy(matrixcopy, matrix, sizeof(int) * 3 * 5);
printf("%d\n", matrixcopy[0][0]);
printf("%d\n", matrixcopy[2][4]);
 

gnasher729

Suspended
Nov 25, 2005
17,980
5,566
I guess I was looking for something like this:

@interface myObject: NSObject
{
. . .
int matrix[3][5];
. . .
}

-(void) setMatrix: (int) newMatrix
{
matrix = newMatrix
}

Since you want to set an array of 3 x 5 elements, that is what you should pass in the setter (Untested code).

-(void) setMatrix: (int [3][5]) newMatrix
{
int i, j;
for (i = 0; i < 3; ++i) for (j = 5; j < 5; ++j)
matrix [j] = newMatrix [j];
}

or:
-void setMatrixElement: (int)newElement atX: (int) x atY: (int) y
{
matrix [x][y] = newElement;
}

and call that 15 times.
 

North Bronson

macrumors 6502
Original poster
Oct 31, 2007
395
1
San José
Since you want to set an array of 3 x 5 elements, that is what you should pass in the setter (Untested code).

-(void) setMatrix: (int [3][5]) newMatrix
{
int i, j;
for (i = 0; i < 3; ++i) for (j = 5; j < 5; ++j)
matrix [j] = newMatrix [j];
}


Great. That's pretty much what I was looking for. Thanks. I was trying to write something like:

-(void) setMatrix: (int) newMatrix[3][5]
{
. . .
}

and it wasn't working. Now I've got it.

I guess the only question is what would be the best way to write the program. I guess I could also just pass the 3 x 5 matrix onto the agent, then have the agent just set a pointer to the matrix instead of actually saving it to the agent object.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.