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 7758235
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T13:18:24+00:00 2026-06-01T13:18:24+00:00

(Edit: put possible solution at end) I’m a C/C++ programmer who is learning Objective

  • 0

(Edit: put possible solution at end)

I’m a C/C++ programmer who is learning Objective C to develop iPhone apps. The programs that I will be writing will deal with large 2d arrays of objects. I’ve read about using NSArray’s of NSArray’s and have some working code, but I’m trying to understand how to use C style arrays to save overhead and to learn what you can and can’t do.

In this fragment MapClass only contains two properties, int x and int y. I have the following code fragment working with a statically defined array of 10×10.

MapClass *arr[10][10];

arr[2][3] = [[MapClass alloc] init];
arr[2][3].x = 2;
arr[2][3].y = 3;

NSLog(@"The location is %i %i", arr[2][3].x, arr[2][3].y);
// Output: "The location is 2 3"

This is an example of doing it with a one dimensional array and calculating where the cell is based on the X and Y:

MapClass **arr = (MapClass**) malloc(10 * 10 * sizeof(MapClass *));

arr[3 * 10 + 2] = [[MapClass alloc] init];
arr[3*10 + 2].x = 2;
arr[3*10 + 2].y = 3;

NSLog(@"The location is %i %i", arr[3*10 + 2].x, arr[3*10 + 2].y);
// Output: "The location is 2 3"

My question is this: How can I malloc my array as a two dimensional array so that I can use arr[2][3] style notation to access it?

Everything I’m trying is generating various errors such as “Subscript requires the size of [your class], which is not constant in non-fragile ABI”.

Can anyone give me a snippit on how to do this? I’ve been reading and experimenting and can’t figure it out. Does my one dimensional array example do anything wrong?

Answer?

After fooling around with xzgyb’s answer, I have the following block working. Anything wrong with it? Thanks!

int dimX = 20;
int dimY = 35;

MapClass ***arr = (MapClass***) malloc( dimX * sizeof(MapClass **));
for (int x = 0; x < dimX; ++x)
{
    arr[x] = (MapClass **) malloc( dimY * sizeof(MapClass*));

}

for (int x = 0; x < dimX; ++x)
{
    for (int y = 0; y < dimY; ++y)
    {
        arr[x][y] = [[MapClass alloc] init];
        arr[x][y].x = x;
        arr[x][y].y = y;

    }
}

for (int x = 0; x < dimX; ++x)
{
    for (int y = 0; y < dimY; ++y)
    {
        NSLog(@"%i %i is %i %i", x, y, arr[x][y].x, arr[x][y].y);

    }
}

// Cleanup
for (int x = 0; x < dimX; ++x) {
    for (int y = 0; y < dimY; ++y) {
        [arr[x][y] release];
    }
}

for (int x = 0; x < dimX; ++x)
{
    free(arr[x]);
}

free(arr);
  • 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-01T13:18:25+00:00Added an answer on June 1, 2026 at 1:18 pm

    Try the followed code:

    MapClass ***arr = (MapClass***) malloc(10 * 10 * sizeof(MapClass *));
    
    for ( int row = 0; row < 10; ++row ) {
        arr[ row ] = (MapClass **)&arr[ row * 10 ];
    }
    
    arr[0][1] = [[MapClass alloc] init];
    arr[1][2] = [[MapClass alloc] init];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Edit: I put this snippet of code in jsbin: http://jsbin.com/eneru I am trying to
EDIT: Duplicate of Should Entity Framework Context be Put into Using Statement? I've been
Is it possible to do that in C# 3 or 4? Maybe with some
EDIT: Ok, so the solution i came up with, is basically count the characters
Is it possible to specify a color in a properties file that has an
I want to write something that will take care of: If possible the int
In .bashrc I have added a dir where I put some scripts. Is that
Is it possible to nest forms in Wicket that are independent of each other?
EDIT: Now a Major Motion Blog Post at http://messymatters.com/sealedbids The idea of rot13 is
EDIT: I used, finally, inotify. As stefanB says, inotify is the thing to use.

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.