Pointer to array of pointers

Discussion in 'iOS Development' started by jbonedev, Jun 7, 2009.

  1. jbonedev

    jbonedev New Member

    Joined:
    May 24, 2009
    Messages:
    44
    Likes Received:
    0
    Device:
    iPhone 3G (Black)
    New to Obj C and not sure if I can/should be using malloc for this, my std C is rusty as it is.

    I want the following:
    Code:
    Foo* ptrA[SIZE]; // An array of pointers
    ...
    for(i = 0; i < SIZE; i++) {
        Foo* ptrB[X][Y]
        prtA[i] = ptrB;
        ....
    }
    
    
    So that after this loop I have:
    ptrA[0] pointing to a two dimensional array of Foo's.
    ptrA[1] pointing to a different two dimensional array of Foo's.
    ...
    ptrA[n] pointing to a different two dimensional array of Foo's.

    My compiler doesn't like this line:
    prtA = ptrB;
    Where it says:
    warning: assignment from incompatible pointer type

    Some lines later I want to do:
    Code:
    for(i = 0; i < SIZE; i++) {
        Foo* myFoos = ptrA[i];
        ...
        for(j = 0; ...) {
            for(k = 0; ...) {
                ...
                [myFoos[j][k] doSomething];
            }
        }
    }
    
    
    This is not just a compiler warning now, this time my compiler gives me an error on this line:
    [myFoos[j][k] doSomething];
    Saying:
    error: subscripted value is neither array nor pointer

    Should I be using malloc or something in the first instance to create the two dimensional array rather than using some value on the stack (probably?)
    In the second, do I just need a cast or is this a bigger issue?

    Years of java have spoiled me with not having to deal with strange pointer syntax.
    I mean everything is a pointer here regardless of the size of the array it is pointing to, so can I just use malloc (sizeof ( (MyClass*) * numElements) ? I.e. the size of a pointer to an instance of my class times the number of elements I'm going to be pointing to? And does Objective C jive okay with malloc or will this cause memory problems - yes I would plan on using free when I'm done. Do I then have to cast everything back to a (MyClass*) when I access it? Ug.

    Confused
    Thanks for any help
  2. junkfist

    junkfist New Member

    Joined:
    Jun 7, 2009
    Messages:
    1
    Likes Received:
    0
    Hi there,

    I'm new to the forums, but I thought I'd give a shot at answering your questions.

    First of all, in your first loop, you are declaring a 2D array on the stack. But that is only going to live till the end of each loop iteration. That is bad! This is because of scoping but also because you are declaring it on the stack. If, however, you allocated the array off the heap (new/malloc), then it would work.

    But why don't you just declare your ptrA to be a 3D array?
    Code:
    Foo* ptrA[SIZE][X][Y];
    
    I mean, unless you need them to be split up for a reason. You are looping through ptrA to make them point to 2D arrays, then later you are already using a triple-nested loop go process each pointer, so... why not just reduce the extra steps you are trying to do?

    Does this help?

    p.s. If you do decide to use malloc (I'm not sure if it's the best way to go in Obj-C code), your malloc should look like:
    Code:
    malloc( sizeof(MyClass*) * numElements );
    
    I think your parenthesis were off.
  3. jbonedev

    jbonedev New Member

    Joined:
    May 24, 2009
    Messages:
    44
    Likes Received:
    0
    Device:
    iPhone 3G (Black)
    Thanks for the reply.

    The array sizes are not homogeneous. I do not know the sizes of the 2D arrays until I start iterating.

    So for ptrA here I do:
    Foo* ptrA = malloc (sizeof ( (MyClass*) ) * SIZE );

    And for the 2d arrays it points to I do
    ptrA[n] = malloc (sizeof ( (MyClass*) ) * X * Y );

    Those parenthesis look right to me btw but maybe I'm wrong?

    Okay when I do:
    Code:
    Foo** ptrA = malloc (sizeof ( (MyClass*) ) * SIZE );
    ...
    ptrA[n] = malloc (sizeof ( (MyClass*) ) * X * Y );
    Foo* ptrB = ptrA[n];
    ...
    ptrB[j][k] = nil; // Compiler complains: error: subscripted value is neither array nor pointer
    
    So how do I solve error: subscripted value is neither array nor pointer. Do I need some kind of cast?

Share This Page