LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

**pointers versus arr2D

I have 2D-string array as the following:
 
char*str_arrDF [2][3];
 
str_arrDF[0][0] = "00";
str_arrDF[0][1] = "01";
str_arrDF[0][2] = "02";
 
str_arrDF[1][0] = "10";
str_arrDF[1][1] = "11";
str_arrDF[1][2] = "12";
 
Can we use with char **str_ptr then calloc & set as the above (or similiar)?
 
Thanks for any help,
0 Kudos
Message 1 of 7
(3,816 Views)
I'm not entirely sure is this is what you are looking for, but I put together a small example of a dynamically allocated 2-d array of dynamically allocated strings initialized as you suggest.

char ***MakeArray(int rows, int cols)
{
    int i, j;
    char ***str_ptr = calloc(rows, sizeof(char **));

    for (i = 0; i < rows; ++i)
        str_ptr[i] = calloc(cols, sizeof(char *));

    for (i = 0; i < rows; ++i)
        for (j = 0; j < cols; ++j)
        {
            str_ptr[i][j] = malloc((i / 10 + j / 10 + 5) * sizeof(char));
            sprintf(str_ptr[i][j], "%d%d", i, j);
        }
    return str_ptr;
}

void FreeArray(char ***str_ptr, int rows, int cols)
{
    int i, j;
   
    for (i = 0; i < rows; ++i)
    {
        for (j = 0; j < cols; ++j)
        {
            free(str_ptr[i][j]);
        }
        free(str_ptr[i]);
    }
    free(str_ptr);
}

int main(void)
{
    int i, j, rows = 2, cols = 3;
    char ***str_ptr = MakeArray(rows, cols);
   
    for (i = 0; i < rows; ++i)
    {
        for (j = 0; j < rows; ++j)
        {
            printf("%s%s", str_ptr[i][j], (j == 0) ? "" : " ");
        }
        printf("\n");
    }
   
    FreeArray(str_ptr, rows, cols);
}

If you have more specific questions, I can go into more detail.

Hope this helps,

-alex
Message 2 of 7
(3,812 Views)
That is exactly what I need, but in reallity, this is the first time I see the use of *** poniter
 
Many thanks Smiley Happy  Smiley Happy  Smiley Happy
0 Kudos
Message 3 of 7
(3,808 Views)
Yes, *** is not very common I suppose. I also got a little confused when I saw it on the post.
I think, here is how it goes:
 
char*   : ['h', 'e', 'l', 'l', 'o'] //an array of chars
char**  : ["hello", "world"] //an array of strings
char*** : ["hello", "world"] //a 2D matrix of strings, where each row is a char**
          ["hello", "world"] //and each cell is a char*
 
That's why in Alex's code, the rows are calloc'd as char** and each cell/entry is calloc'd as char*.
In char***, first 2 *'s stand for the row and column respectively and the 3rd * is for the character array in the specific cell.
But I cannot understand the line:
 
str_ptr[i][j] = malloc((i / 10 + j / 10 + 5) * sizeof(char));
 
Why use a malloc and what's 5 and 10?

Message Edited by ebalci on 04-02-2007 11:19 AM

S. Eren BALCI
IMESTEK
0 Kudos
Message 4 of 7
(3,779 Views)
            str_ptr[i][j] = malloc((i / 10 + j / 10 + 5) * sizeof(char));
            sprintf(str_ptr[i][j], "%d%d", i, j);


In this code, we're allocating the character buffer and writing the string into it (string is just the row and column numbers, done because that is what the original post did). The slightly funny looking formula is just to compute how large the buffer needs to be (if the row/col numbers exceed one digit). (i/10) is (digits - 1) for the rows and (j/10) is (digits - 1) for the cols. The 5 represents: one character for each of two possible negative signs (just me being paranoid), one character for the first digit of each number (since i/10 yields digits -1, have to add one for both numbers) and one character for the null terminator. If you happen to know the possible range of rows and cols, you could make this a lot simpler, but this was written to work for any number of rows/cols.

In retrospect, the 5 could be a 3 because, if rows or cols were negative the loop would never be executed anyway (not to mention that i/10 and j/10 would sometimes be negative, which would break the code anyway).

The reason to use malloc is essentially because in the original code, all the possible states were enumerated. In my code, there is just a loop, so you cannot declare the arbitrary number of needed static strings ("00", "01", etc.).

Regards,

-alex

0 Kudos
Message 5 of 7
(3,756 Views)
Hi Alex.

Sorry, I can't help myself 🙂

This:
    str_ptr[i][j] = malloc((i / 10 + j / 10 + 3) * sizeof(char));
will allocate more memory than required if (i >= 20) or (j >= 20).

What's needed is something like this:
    str_ptr[i][j] = malloc((log10(i) + log10(j) + 3) * sizeof(char));

I feel better now.

Regards,
Colin.
0 Kudos
Message 6 of 7
(3,738 Views)
Haha. Good point. Amusing that I did it twice and still didn't notice. Whee!
0 Kudos
Message 7 of 7
(3,733 Views)