Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7915179
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T14:22:19+00:00 2026-06-03T14:22:19+00:00

Is there a way to declare a 2D array of integers in two steps?

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T14:22:24+00:00Added an answer on June 3, 2026 at 2:22 pm

    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]);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a way to declare array elements volatile in Java? I.e. volatile int[]
Is there a way to declare an unsigned int in Java? Or the question
Is there a way to declare first and then initialize an array in C?
I've been trying to come up with a way to declare array constants in
Is there a simple way to declare CSS classes for Symfony form labels? This
I want to declare a string array, I was using this way of doing
Is there a way to declare that the variable type of a generic class
Is there any way can declare a bean in just like JSP UseBean in
Is there a way to declare which signals are subscribed by a Python application
Is there a way to declare a variable with a particular user-defined type in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.