C arrays in Objective-C

Discussion in 'Mac Programming' started by North Bronson, Oct 20, 2008.

  1. North Bronson macrumors 6502

    Joined:
    Oct 31, 2007
    Location:
    San José
    #1
    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?
     
  2. Cromulent macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #2
    Same way you would in pure C. Loop through the array making the assignments.
     
  3. North Bronson thread starter macrumors 6502

    Joined:
    Oct 31, 2007
    Location:
    San José
    #3
    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.
     
  4. Cromulent macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #4
    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.
     
  5. North Bronson thread starter macrumors 6502

    Joined:
    Oct 31, 2007
    Location:
    San José
    #5
    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.
     
  6. SydneyDev macrumors 6502

    Joined:
    Sep 15, 2008
    #6
    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]);
    
     
  7. gnasher729 macrumors P6

    gnasher729

    Joined:
    Nov 25, 2005
    #7
    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.
     
  8. North Bronson thread starter macrumors 6502

    Joined:
    Oct 31, 2007
    Location:
    San José
    #8


    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.
     

Share This Page