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?

5
  • Did you try int ** myInts;? Commented Oct 26, 2011 at 12:47
  • Michael Kerlin: Sorry for not beeing as smart as you :/ Commented Oct 26, 2011 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); Commented Oct 26, 2011 at 12:56
  • 1
    Look at this or many many other posts you can find in StackOverflow just by searching 2d array allocation C Commented Oct 26, 2011 at 13:02
  • Thanks, again, sorry for my inability to succeed at life Commented Oct 26, 2011 at 13:29

3 Answers 3

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!

Sign up to request clarification or add additional context in comments.

Comments

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

2 Comments

Thanks, but this is for a game so I don't wanna use a bunch of objects if I don't have to, for performance.
So do you need C-style realization?
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.