dynamically setting size of 2d char array

This is a discussion on dynamically setting size of 2d char array within the C Programming forums, part of the General Programming Boards category; How do i go about dynamically setting size of a 2d char array. is this right? int numcols = 2; ...

  1. #1
    Waxy-Dock
    Join Date
    Mar 2005
    Posts
    69

    dynamically setting size of 2d char array

    How do i go about dynamically setting size of a 2d char array.

    is this right?

    int numcols = 2;
    int numrows = 2;

    char *my2darray = malloc(numcols*numrows); /* since size of char is 1 */

    /* now can i do something like this? */
    my2darray[0][1] = 'a';
    my2darray[1][1] = 'b';

    free (my2darray); etc..

  2. #2
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    Not quite. Look at:
    Code:
    my2darray[0][1] = 'a';
    The compiler has no idea how long a row/column is, and thus such statements won't work. You can still access the pointer as if it were a 1d array - you'll have to compute the index yourself:
    Code:
    my2darray[row_you_want * num_of_columns + column_you_want]
    or
    Code:
    my2darray[column_you_want * num_of_rows + row_you_want]
    Use one or the other, depending on if you have any preference as to how the data is laid out in memory.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  3. #3
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    Code:
    #include <stdio.h>
    
    int main()
    {
        char** myArray;
        int numCols = 2;
        int numRows = 2;
        
        int i;
        
        myArray = malloc(sizeof(char*) * numRows);
        
        for (i = 0; i < numRows; i++)
            myArray[i] = malloc(numCols);
        
        // when done working with myArray
        
        for (i = 0; i < numRows; i++)
            free(myArray[i]);
            
        free(myArray);
        
    }
    does this help?

  4. #4
    Day Dreamer
    Join Date
    Apr 2007
    Posts
    45
    Quote Originally Posted by nadroj View Post
    Code:
    #include <stdio.h>
    
    int main()
    {
        char** myArray;
        int numCols = 2;
        int numRows = 2;
        
        int i;
        
        myArray = malloc(sizeof(char*) * numRows);
        
        for (i = 0; i < numRows; i++)
            myArray[i] = malloc(numCols);
        
        // when done working with myArray
        
        for (i = 0; i < numRows; i++)
            free(myArray[i]);
            
        free(myArray);
        
    }
    does this help?


    That's the right way to do it.
    You need to allocate memory for each row.

  5. #5
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    That's the right way to do it.
    You need to allocate memory for each row.
    That's one way to do it (which I forgot about) - which you want depends on your needs/wants. Bitmap data would usually be represented in something similar to my post - strings could be implemented either way. (Although the second is probably the more flexible/readable, but requires more calls to malloc().) As with anything, see which best fits the task at hand - and (most importantly) know/document how you choose to organize your data, and how any functions you use to manipulate that data expect it to be represented. (Especially if some other code not under your control works with your data.)

    One of the CBoard contests had a slight issue with people choosing differently between row/column major tables once... some used row major, some column (so that it was [x][y])
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Heapsort
    By xENGINEERx in forum C Programming
    Replies: 2
    Last Post: 03-30-2008, 07:17 PM
  2. finding size of empty char array
    By darsunt in forum C Programming
    Replies: 12
    Last Post: 05-30-2006, 07:23 PM
  3. Trouble with DMA Segmentation Faults
    By firestorm717 in forum C Programming
    Replies: 2
    Last Post: 05-07-2006, 09:20 PM
  4. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM
  5. comparing fields in a text file
    By darfader in forum C Programming
    Replies: 9
    Last Post: 08-22-2003, 08:21 AM

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