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

echel0n

macrumors newbie
Original poster
Sep 29, 2008
11
2
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!
 

marcpage

macrumors newbie
Oct 14, 2008
6
0
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} };
 

saMac

macrumors newbie
Oct 13, 2008
21
0
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...
 

echel0n

macrumors newbie
Original poster
Sep 29, 2008
11
2
Guys,

Thanks for the quick replies.

marcpage:

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} };

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:

instead of the "int _map[][]" thing, why not use a double pointer, like
"int ** _map" ?

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...
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.