c-arrays and struct

Discussion in 'Mac Programming' started by MrFusion, Sep 12, 2007.

  1. MrFusion macrumors 6502a

    Jun 8, 2005
    Being a (mostly) self taught programmer, I am not sure what the good practices are concerning struct's and c-array's.
    I know how to define a struct and arrays inside a function.

    typedef struct {
    	double x;
    	double y;
    	double z;
    } position;
    @implementation MyClass :NSObject {
    -(void) test {
        position somePositions[3][2] = {{{1,2,3},{3,4,5}},{{1,2,3},{3,4,5}},{{1,2,3},{3,4,5}}}
     somePositions[1][2].z = 44;
    But not how to declare one of unknown length in the @interface.
    @interface MyClass :NSObject {
        position allpositions[]
    Yes, there are NSMutableArray's, but these are not very convenient for things like storing matrices (or doing matrix calculations).

    All help and lessons are appreciated.

    PS. Unfortunately I don't have my obj-c book close by for the next several days.
  2. kpua macrumors 6502

    Jul 25, 2006
    Arrays in C are accessed simply by using a pointer to the first element in the array. Hence the following are essentially equivalent:

    int myArray[ARRAY_LENGTH];
    int * myArrayPtr;
    myArray is a pointer to ARRAY_LENGTH words allocated either in static memory or the stack.
    myArrayPtr is a pointer to nothing... yet. Allocating that memory is up to you.

    C doesn't let you use the [] syntax to declare arrays of dynamic size. You have to declare such an array as a pointer and malloc memory for it at runtime. Once you have the memory allocated, you can use the [] syntax to access elements of the array just as you normally would.
  3. MrFusion thread starter macrumors 6502a

    Jun 8, 2005

    and in the case of a matrix? How is such a beast created?
    mat[][] to access.
  4. Krevnik macrumors 68040


    Sep 8, 2003
    Pointer to a pointer:

    int **myMatrixPtr;

    Allocating it is another matter entirely:
    myMatrixPtr = malloc( sizeof(int *) * xDimension );
    for( i = 0; i < xDimension; i++ )
        myMatrixPtr[i] = malloc( sizeof(int) * yDimension );
    And then there is the nightmare of freeing that bastard.
  5. garethlewis2 macrumors 6502

    Dec 6, 2006
    You can pass just using

    functionname(struct[][3], int size)

    As C only need to one of the dimensions for memory access. I believe it is that one I haven't got my K&R book with me.

    Why are you trying to do this with structures. Your using Objective-C, why don't you put the dim struct into class, which is semantically identical to a structure anyway. Then stick this into a two dimensional NSArray. You can then forget about memory problems as long as you understand reference counting.
  6. AlmostThere macrumors 6502a

  7. garethlewis2 macrumors 6502

    Dec 6, 2006
    If he did that, his code will completely and totally unportable. Not to mention unless he puts a specific line in the readme for how to configure the compiler, code written with these flags in mind would never compile anywhere else.
  8. kainjow Moderator emeritus


    Jun 15, 2000
    Well since he's using Cocoa, it doesn't look like that will be much of a problem ;)
  9. AlmostThere macrumors 6502a

    As opposed to avoiding compatibiltiy issues by ignoring ISO standards and putting all your compiler flags in README files rather than say, a makefile?
  10. Krevnik macrumors 68040


    Sep 8, 2003
    I still think the easiest and simplest way (and still have high perf) is to use a struct with a handle in it:

    typedef struct {
      int x, y;
      int **data;
    } MrMatrix;
    MrMatrix *CreateMatrix(int x, int y) {
      // Allocate here, storing x and y into the struct
    void FreeMatrix(MrMatrix *matrix) {
      // Break down here, using x and y to help guide how to dealloc the matrix
    // Access matrix example using this template:
    MrMatrix *myMatrix;
    myMatrix = CreateMatrix(3, 3);
    Easy, portable, and your data access will be faster than using NSArray, without stepping outside ISO.
  11. lazydog macrumors 6502a

    Sep 3, 2005
    Cramlington, UK

    If you don't mind running your code throught the Objective-C++ compiler you could implement your matricies using C++ classes (and leave the rest of the code in Objective-C). This would allow you to define operators for matrix addition, scalar multiplication etc. Whether this would be of any help or interest to you I don't know. Anyway, if you were to do it this way then you might want to define a column vector class and use this as the basis for your matrix class and operators.

    b e n
  12. MrFusion thread starter macrumors 6502a

    Jun 8, 2005
    I haven't yet taking the time to delve into xCode and its compiler settings etc.
    So I rather not do anything that requires anything beyond cmd-B or cmd-R.
    Certainly not .make files or different compilers. It's probably easy to do, but I need to alloc time for it.

Share This Page