Like Tree1Likes
  • 1 Post By Tclausex

Dynamic char array allocation and assignment

This is a discussion on Dynamic char array allocation and assignment within the C Programming forums, part of the General Programming Boards category; I'm allocating memory for a 8X8 char array: Code: char **game; game = (char**)malloc(8 * sizeof(char*)); for(i = 0; i ...

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    16

    Dynamic char array allocation and assignment

    I'm allocating memory for a 8X8 char array:

    Code:
    char **game;
    game = (char**)malloc(8 * sizeof(char*));
    for(i = 0; i < 8; i++)   
       game[i] = (char*)malloc(8 * sizeof(char));
    then try to assign a character from a char array to each column in the first row game[0][0] = r, game[0][1] = b and so on...

    Code:
    char myArray[8] = "rbnqknbr";
    i=0;
    for(int j=0; j<8; j++)   
       game[i][j] = myArray[j];
    Code compiles with no errors but the cmd line crashes when I run the program. Any thoughts?

  2. #2
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    You should be checking return value from malloc ; other than that, more of code might be revealing.

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    16
    Can you explain this a bit more? check the return value of malloc?
    Thats my code, at least thats where I'm stuck with my exercise.

    this:
    Code:
    game[0][0] = 'r';
    game[0][1] = 'b';     
    game[0][2] = 'n';
    game[0][3] = 'q';     
    game[0][4] = 'k';     
    game[0][5] = 'n';     
    game[0][6] = 'b';     
    game[0][7] = 'r';
    has the same result btw.

  4. #4
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    malloc - C++ Reference Read the Return Value entry.

    If that is all of your code, then it runs fine for me. With the exception of declaring j inside the for loop, no problems. Paste your whole program in.

  5. #5
    Registered User
    Join Date
    Oct 2011
    Posts
    16
    Duh, right. I was allocating the memory in a function like that.

    Code:
    void allocate_memory(char **game)
    {
        //char **game;
        game = (char**)malloc(8 * sizeof(char*));
        int i;
        for(i = 0; i < 8; i++)
           game[i] = (char*)malloc(8 * sizeof(char));      
    }
    and calling it in main like that

    Code:
     
    allocate_memory(game);
    Then continue with the code in my first post in main.

    So the question changes to: How do I call a function with a parameter with double pointer and the & (umpersand)? Is this the right way to do it? passing the parameter by reference?
    Last edited by mnml; 11-16-2011 at 11:47 AM.

  6. #6
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    So to explain what's going on in your program (and if I say it wrong, I'm sure someone will slap my hand) :

    C doesn't have a true pass by reference. That is, game in your function is not some alias to game in main. It is a local variable, a pointer to pointer that expects an address as argument for the function.
    When you call allocate_memory(game), game is evaluated for it's value (an address to a char pointer) which is passed to the function and stored in the function's variable game. So is game pointing to a char pointer when you call the function?

    All that said, the simple solution is to not pass any values, but to return a pointer to pointer.

  7. #7
    Registered User
    Join Date
    Oct 2011
    Posts
    16
    Thanks for your reply and explanation.
    Do you mean that I should return a pointer to pointer inside allocate_memory() function?
    How do I implement that?

    all my code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    
    void allocate_memory(char **game)
    {
        game = (char**)malloc(8 * sizeof(char*));
        int i;
        for(i = 0; i < 8; i++)
           game[i] = (char*)malloc(8 * sizeof(char));      
    }
    
    void main ()
    {
         char **game;
         char pawns[8] = "rbnqknbr";
    
         allocate_memory(game);
         for(int j=0; j<8; j++)
             game[0][j] = pawns[j];
    }

  8. #8
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    Code:
    char ** allocate_memory( void )
    {
    char **ptr = {...} ;
    ...
    return ptr ;
    }
    Change the prototype of the function
    dheaven likes this.

  9. #9
    Registered User
    Join Date
    Mar 2009
    Posts
    343
    Or alternatively, use an additional level of pointers to emulate pass by reference :

    Looks complex, but you're basically just replacing every instance of game in allocate_memory with *game. The only tricky part is you need (*game)[i] since *game[i] means something different (specifically, take game[i] as a pointer and dereference it, which is different from what you want - take game as a pointer, dereference it to get an array, and then get entry i from that array).

    No real reason to pick this versus the "return a pointer" approach until you have a function which allocates more than one pointer. Since you can only return one value, you'd need a different approach in that case.

    Code:
    void allocate_memory(char ***game)
    {
       *game = malloc(8 * sizeof(char*));
        int i;
        for(i = 0; i < 8; i++)
           (*game)[i] = malloc(8 * sizeof(char));     
    }
     
    int main ()
    {
         char **game;
         char pawns[8] = "rbnqknbr";
     
         allocate_memory(&game);
         for(int j=0; j<8; j++)
             game[0][j] = pawns[j];
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 02-23-2010, 05:17 AM
  2. Dynamic array Allocation
    By deepak_j in forum C Programming
    Replies: 3
    Last Post: 08-17-2009, 07:18 AM
  3. dynamic memory allocation char array
    By waxydock in forum C Programming
    Replies: 2
    Last Post: 05-12-2007, 07:05 AM
  4. Dynamic allocation for array of pointers to char
    By bivhitscar in forum C Programming
    Replies: 7
    Last Post: 05-20-2006, 07:04 AM
  5. Classes and char* and Dynamic memory allocation
    By Bozz in forum C++ Programming
    Replies: 3
    Last Post: 02-16-2003, 02:01 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21