Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Google Sign In with OpenID
Please do not post the same thing multiple times. The board software automatically flags certain posts as needing moderator attention. This happens the most often for new users. I'm pretty sure this is made clear at the time you attempt to post. Posting the same thing over and over again just makes that many more posts the moderators have to weed through later. This makes us sad. Don't make us sad. If your post/thread doesn't appear, just wait a while. Don't post it again. If it hasn't shown up by the next day, then you can try again. I normally go through posts in the mornings, and try to check a few times throughout the day, but I'm not here 24/7. There will typically be a significant delay before posts are approved. Just be patient.

Problem with char array

Hello,

In my .h file, i have a 2d char array like this:
char grid [10][10]

Everything is fine but i can't change the size like:
grid = new grid[2][7]

I need to access the "grid" from everywhere in my .m file and i know the size only in the .m file.

What can i do ?

Replies

  • QuantumDojaQuantumDoja Posts: 623Registered Users
    One idea would be to keep two variables, one for x & y, set x to 2 and y to 7 and use those instead of numbers.
    www.itheme.com
    deal.gameweaver.com
    guessem.gameweaver.com
  • LevyMastLevyMast Posts: 97Registered Users
    kamax said:

    Hello,

    In my .h file, i have a 2d char array like this:
    char grid [10][10]

    Why are you declaring global variables in your header files?
    There's something wrong with that.

    If you want to access C/C++ data from Objective-C, then you should put your C/C++ data into a class,
    then dynamically allocate a pointer to it. Then access your data through that pointer.
  • Duncan CDuncan C Posts: 8,038Tutorial Authors, Registered Users
    kamax said:

    Hello,

    In my .h file, i have a 2d char array like this:
    char grid [10][10]

    Everything is fine but i can't change the size like:
    grid = new grid[2][7]

    I need to access the "grid" from everywhere in my .m file and i know the size only in the .m file.

    What can i do ?

    Those are 2 different issues.

    If you want to be able to resize a C array, you need to create it with malloc and release it with free:
    char *grid;
    int height = 10;
    int width = 10;

    malloc(grid);
    Then you have to release it with free:

    free(grid);

    To resize, you'd have to free the old memory and malloc a new array.

    Then you have to use index math to address the memory:
    grid[x + y+width];
    Since the above approach has a height and a width, you can expose those to the rest of your program.

    Obviously it would be better if the grid was a char* property and height and width were readonly properties, and you had a grid_resize method.
    Regards,

    Duncan C
    WareTo

    mug

    Animated GIF created with Face Dancer, available for free in the app store.
  • LevyMastLevyMast Posts: 97Registered Users
    kamax said:

    Hello,

    In my .h file, i have a 2d char array like this:
    char grid [10][10]

    Everything is fine but i can't change the size like:
    grid = new grid[2][7]


    If you want to be lazy about it, declare a 2D array with a max dimensions:

    #define MAX_X 10
    #define MAX_Y 10
    char grid [MAX_X][MAX_Y];


    Then use variables to control how large you want your array:

    int max_x = 5;
    int max_y = 7;


    Your code should work the same as before.
    Post edited by LevyMast on
  • kamaxkamax Posts: 7New Users
    Thank you for your help. I have to say Objective C is very new for me, i'm more experienced with java.

    The lazy way was my first idea, just size the grid to the max and use only a part of it. This should work but i don't want to use "bad coding way".

    The idea to put the grid into a class sounds better, i will look on how to do it but i suppose i should use Duncan C method to assign the grid size in this class. Looks not so easy...

    Edit:
    I just remember that i must think Objects. I will try to make my grid a object with methods like getValueOf[x][y] and setValueOf[x][y] (if this is possible like in java).
    Post edited by kamax on
Sign In or Register to comment.