1

Is there a way to declare a 2D array of integers in two steps? I am having an issue with scope. This is what I am trying to do:

//I know Java, so this is an example of what I am trying to replicate:

int Array[][];
Array = new int[10][10];

Now, in OBJ-C I want to do something similar, but I cant get the syntax right. Right now I have it in one step, but I cannot use it outside of the If-Statement in which I currently have it:

int Array[10][10]; //This is based on an example I found online, but I need 
                   //to define the size on a seperate line than the allocation

Can anyone help me out with this? I know its probably a more basic question, but you can't use the keyword "new" outside of a message (to my knowledge) and you cant send messages to ints. :(

*EDIT 1:**

My problem is scope related.

//Declare Array Somehow
Array[][] //i know this isn't valid, but I need it without size

//if statement
if(condition)
Array[1][2]
else
Array[3][4]

//I need to access it outside of those IFs

//... later in code
Array[0][0] = 5;
1
  • You shouldn't use new at all in Objective-C. Commented May 7, 2012 at 16:20

2 Answers 2

4

This is my preferred way of creating a 2D array, if you know the size of one of the boundaries:

int (*myArray)[dim2];

myArray = calloc(dim1, sizeof(*myArray));

And it can be freed in one call:

free(myArray);

Unfortunately, one of the bounds MUST be fixed for this to work.

However, if you don't know either of the boundaries, this should work too:

static inline int **create2dArray(int w, int h)
{
    size_t size = sizeof(int) * 2 + w * sizeof(int *);
    int **arr = malloc(size);
    int *sizes = (int *) arr;
    sizes[0] = w;
    sizes[1] = h; 
    arr = (int **) (sizes + 2);

    for (int i = 0; i < w; i++)
    {
        arr[i] = calloc(h, sizeof(**arr));
    }

    return arr;
}

static inline void free2dArray(int **arr)
{
     int *sizes = (int *) arr;
     int w = sizes[-2];
     int h = sizes[-1];

     for (int i = 0; i < w; i++)
         free(arr[i]);

     free(&sizes[-2]);
}
Sign up to request clarification or add additional context in comments.

2 Comments

The first method worked for me. (Lucky me had 1 fixed dimension!!) Thanks so much!!
this is a beautiful solution to creating variable-size 2d arrays. do note that h is unused in free2dArray(•)
0

The declaration you showed (e.g. int Array[10][10];) is OK, and will be valid for the scope it was declared to, if you do it in a class scope, then it will be valid for the whole class.

If the size of the array varies, either use dynamic allocation (e.g. malloc and friends) or use NSMutableArray (for non-primitive data types)

4 Comments

I need access to it outside of the scope. The size of the array varies based on an If-Else statement set, but then I can't use the variable outside of whichever IF/Else is used.
I edited the main post to better describe my question, but I suppose I could use an NSMutableArray. I am working solely with "int" values though, so I was trying to stick with primitive types so that I don't have to constantly send messages for [intValue] and such
@MrHappyAsthma You should NOT use a NSArray for storing integers, it's not intended for that. You should always stick to using C arrays for that kind of stuff, or a C++ vector / array.
That is what I assumed was best. I just can't figure out a way to satisfy my situation with a primitive array. I don't know the size of the array until the conditional IF statements, but I still need access to it outside of the IF statement scope.

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.