0

How would you go about storing a 2 dimensional array of ints as a class variable?

If you want an array of ints you go:

Class declaration

int * myInts;

Implementation

int ints[3] = {1,2,3};
myInts = ints;

But what if you want to store an array of arrays with ints?

Like this:

 int ints[3][3] = {{1,2,3}, {1,2,3}, {1,2,3}};

I don't wanna limit the size of the arrays in the class declaration so I guess I have to go with pointers, but how?

oskob
  • 1,386
  • 1
  • 11
  • 27
  • Did you try `int ** myInts;`? – Hot Licks Oct 26 '11 at 12:47
  • Michael Kerlin: Sorry for not beeing as smart as you :/ – oskob Oct 26 '11 at 12:54
  • Daniel R Hick: How would you then fill this double pointer with actual ints? you use malloc(sizeof(int)*numInts) if you have an ordinary pointer, but what do I do with this? int ** myInts = malloc(sizeof(??)*numArrs); – oskob Oct 26 '11 at 12:56
  • 1
    Look at [this](http://stackoverflow.com/questions/1824363/dynamic-allocation-deallocation-of-2d-3d-arrays) or many many other posts you can find in StackOverflow just by searching `2d array allocation C` – Shahbaz Oct 26 '11 at 13:02
  • Thanks, again, sorry for my inability to succeed at life – oskob Oct 26 '11 at 13:29

3 Answers3

1

For future reference, this is my conclusion:

Class declaration

 int ** ints;

Implementation

 int rows = 2;
 int cols = 5;

 ints = (int**)malloc(rows*sizeof(int*));
 ints[0] = (int*)malloc(cols*sizeof(int));

 ints[0][0] = 123;
 ints[0][1] = 456;
 ints[0][2] = 789;
 // etc

This is my own interpretation of links provided in comments and my C skills are pretty low so take that into consideration ;) Maybe there are better ways to put in multiple numbers at a time with {123,456,789} or something, but that is beyond my requirements for now!

oskob
  • 1,386
  • 1
  • 11
  • 27
0

If you want to dynamically allocate memory, in other words define the size of the arrays at runtime, then you need to declare the array as a pointer, malloc it, and then add another array of ints to each index at runtime. You can't really declare and dynamically allocate at the class level. If you are using cocoa/iphone sdk you can use NSMutableArray.

You could also create your own class that constructs a two dimensional array and exposes methods to push and pop int objects like [IntegerArray push:x,y,n];

Here's and example of using a double reference as Daniel R Hicks pointed out.

Community
  • 1
  • 1
Daniel Pereira
  • 1,785
  • 12
  • 10
0

I've wrote sample for you:

int N = 10, M = 15;
NSMutableArray *ints = [NSMutableArray arrayWithCapacity:N]; // array[N][M]
for (int i=0; i<N; i++)
{
    NSMutableArray *arr = [NSMutableArray arrayWithCapacity:M];
    for (int j=0; j<M; j++)
    {
        [arr addObject:[NSNumber numberWithInt:(i+1)*(j+1)]];
    }
    [ints addObject:arr];
}
// print
for (int i=0; i<[ints count]; i++)
{
    NSString *line = @"";
    NSMutableArray *arr = [ints objectAtIndex:i];
    for (int j=0; j<[arr count]; j++)
        line = [NSString stringWithFormat:@"%@ %@", line, [arr objectAtIndex:j]];
    NSLog(@"%@", line);
}
Nekto
  • 17,837
  • 1
  • 55
  • 65