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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:33:34+00:00 2026-05-23T16:33:34+00:00

is it possible to declare a variable length array with global scope in objective-c?

  • 0

is it possible to declare a variable length array with global scope in objective-c?

I’m making a game with a world class, which initializes the world map as a three dimensional integer array. while it’s only a two dimensional side scroller, the third dimension of the list states which kinda of block goes at the coordinate given by the first two dimensions

after the initialization function, a method nextFrame: is scheduled (I’m using cocos2d and the CCDirector schedule method). I was wondering how to pass the int[][][] map array from the initialization function to the nextFrame function

I tried using global (static keyword) declaration, but got an error saying that global arrays cannot be variable length

the actual line of code I’m referring to is:

int map[xmax][ymax][3];

where xmax and ymax are the farthest x and y coordinates in the list of coordinates that defines the stage.

I’d like to somehow pass them to nextFrame:, which is scheduled in

[self schedule:@selector(nextFrame:)];

I realize I can use NSMutableArray, but NSMutableArray is kinda a headache for 3-dimensional lists of integers (I have to use wrapper numbers for everything…). is there any way to do this with integer arrays?

  • 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-05-23T16:33:35+00:00Added an answer on May 23, 2026 at 4:33 pm

    You can’t have a statically allocated global array of dynamic dimensions in C (of which Objective C is a clean superset). But you can use a global array of any length or size (up to available memory) at runtime by using a global pointer, malloc, and array indexing arithmetic.

    static int *map = NULL;
    
    ...
    map = malloc (dim1 * dim2 * dim3 * sizeof(int));  // in some initialization method
    
    if (map == NULL) { /* handle error */ }    // before first array access
    
    ...
    myElement = map[ index3 + dim2 * ( index2 + dim1 * index1 ) ];  // some macro might be suitable here
    

    Or you could make Objective C getter and setter methods that checks the array and array bounds on every access, since a method can return plain C data types.

    Another option, if you know the max dimensions you want to have available and are willing to use (waste) that amount of memory, is to just statically allocate the max array, and throw an exception if the program tries to set up something larger than your allowed max.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to declare a variable which holds a class which implements a specific
I was wondering if it is possible to declare an array (size not known
Is it possible to declare a variable in Python, like so?: var so that
Is it possible to declare a variable in c++ without instantiating it? I want
Correct me if I am wrong, but isn't it possible to declare a variable
why or for what reason is it not possible to declare a class member
In Objective-C when you declare an instance variable you can check if it conforms
Possible Duplicate: Why can't I create an array with size determined by a global
Is it possible to declare an array of pointers and later on initialize either
Is it possible to declare global variables in MATLAB inside a loop: cellvar =

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.