Objective C: Using multidimensional C arrays

Discussion in 'iOS Programming' started by echel0n, Oct 14, 2008.

  1. echel0n macrumors newbie

    Joined:
    Sep 29, 2008
    #1
    Hi folks,

    My first post here - new to the whole Mac/iPhone/ObjC world.

    I am trying to access a C++ multidimensional array from Objective C. The array is large and autogenerated from a previous project and it is not really convenient to change it into an NSArray.

    The nub of the problem is this: the compiler complains at the following code:-

    Code:
    @interface Texture2D : NSObject
    ...
      int               _map[][4];
    ...
    @end
    
    The message is: "error: instance variable '_map' has unknown size."

    Now I can see where it's coming from with that, but this particular line compiles quite happily in both ANSI C and - more pertinently - in a C++ class header file, because at the end of the day _map is a pointer, which is of very specific size

    Incidentally, if I change the declaration to this:-

    Code:
    @interface Texture2D : NSObject
    ...
      int               (*_map)[4];
    ...
    @end
    
    Then it compiles and works fine, but I feel that is a bit of a nasty hack and leaves me not fully understanding why Objective C doesn't handle normal C/C++ array syntax, and that I'll probably run into more trouble further down the line.

    Any thoughts on this would be appreciated!
     
  2. marcpage macrumors newbie

    Joined:
    Oct 14, 2008
    #2
    C does not allow you to declare a concrete instantiation without the size. You might be able to "extern" it, but you do need to declare it somewhere else with an actual size (or initialize it with a size, like :

    int _map[][4]= { {1,2,3,4}, {1,2,3,4} };
     
  3. saMac macrumors newbie

    Joined:
    Oct 13, 2008
    #3
    i'm not an objC guy, but i recently spent some time with 2-D arrays in C, and here's what i figure:

    instead of the "int _map[][]" thing, why not use a double pointer, like
    "int ** _map" ? as long as you then allocate memory space for the addresses, i figure that would work...
     
  4. echel0n thread starter macrumors newbie

    Joined:
    Sep 29, 2008
    #4
    Guys,

    Thanks for the quick replies.

    marcpage:

    Sure, the array is defined and initialized exactly like that, elsewhere in a C++ class. The problem is I need a pointer to it in my Objective C class, and the compiler does not want to allow to me declare it in the "int _map[][4]" form. The declaration should not need an extern since I'm passing it directly in a method call which then assigns to my _map variable.

    saMac:

    Yes, I tried that initially but ran into trouble when trying to assign to the variable - conflicts between the different notations, I think. The "int (*_map[4])" solution I found was the only one that seemed to work and it did leave me feeling I'd just got lucky rather than actually understanding the problem.

    I'm wondering if this is to do with the hidden pointer stuff that goes on under the hood with C/C++ arrays. But Objective C claims to be a strict superset of C, so I don't see how it could be different. So more likely it is some limitation of the OO architecture.

    Another possibility is that, although I was weaned on C, I may have been using Java for so long I've forgotten the complex subtleties of C arrays...
     

Share This Page