Hi all;

I've to use a dynamic array(2D) in my program in C.

I've read some tutorials about this and I understood its mental.But still there exist a few points which I didn't understand.

Here is a code:

int main(){
int **a, x;

a = (int **)malloc(sizeof(int) * 10);
for(x = 0; x < 10; x ++) {
    a[x] = (int *)malloc(sizeof(int) * 3);
}

/* ... */

for(x = 0; x < 10; x ++) {
    free(a[x]);
}
free(a);

Firstly: Does the **a mean that first * points the row and second * points the column???(If not what does it mean???)

Secondly: In the "a[x] = (int *)malloc(sizeof(int) * 3);" , malloc function returns an adress and as far as I can see it assigns a[x] to a[x] 's adress.
But when I print the a[x] value and it's adress; I saw it doesn't process like my idea which was indicated above.So what is the problem???

Thanks for your helps...

>Does the **a mean that first * points the row and second * points the column???
Almost. The first * points to a row, however the second * points to an individual "cell" if you will, of a row. It points to the integer.

>malloc function returns an adress and as far as I can see it assigns a[x] to a[x] 's adress.
It assigns the address of the freshly-allocated memory to a[x], yes.

>But when I print the a[x] value and it's adress; I saw it doesn't process like my idea which was indicated above.
What do you mean? a[x] just holds a pointer to the actual memory, you'll have to dereference the address contained inside a[x] if you want to print out the value.

I accept I babbled a little but when I thought the 2D arrays' and pointers' mental I really understood it.
Thanks for your helps...:cool:

a = (int **)malloc(sizeof(int) * 10);
for(x = 0; x < 10; x ++) {
    a[x] = (int *)malloc(sizeof(int) * 3);
}

In this case you were lucky enough to have the size of integer the same as the size of a pointer. If it would have been array of doubles, you would have messed big time. a = malloc(sizeof(int*) * 10); A better way would have been:

int main()
{
    const int SIZE = 10;

    double **arr = (double**)malloc(SIZE * sizeof(*arr));
    for(int i = 0; i < SIZE; ++i)
    {
        arr[i] = (double*)malloc(SIZE * sizeof(*arr[0]));
    }

    for(int i = 0; i < SIZE; ++i)
        for(int j = 0; j < SIZE; ++j)
            arr[i][j] = i * j + i + j;

    for(int i = 0; i < SIZE; ++i)
    {
        for(int j = 0; j < SIZE; ++j)
            cout << arr[i][j] << "\t";
        cout << '\n';
    }
    getchar();
    return 0;
}

Just to let you know that the old smileys are deprecated, you are better off using the new ones, the ones which recent compiler..err Daniweb currently supports... ;-)

Just to let you know that the old smileys are deprecated, you are better off using the new ones, the ones which recent compiler..err Daniweb currently supports... ;-)

So, uh, in other words, don't follow your lead?? ;)

Just to let you know that the old smileys are deprecated, you are better off using the new ones, the ones which recent compiler..err Daniweb currently supports... ;-)

Nooooo..... :icon_biggrin:

start quote:

const int SIZE = 10;
double **arr = (double**)malloc(SIZE * sizeof(*arr));
for(int i = 0; i < SIZE; ++i)
{
    arr[i] = (double*)malloc(SIZE * sizeof(*arr[0]));
}
}

end quote.

This is first allocating an array of pointers, and allocating memory one by one for each one dimensional
array. (this kind of array is called a jagged array in C#).
A true two dimensional array is rectangular; a contigous area of memory which contains I*J elements.
we simulate this in C by creating an array, every element of which is an array. for example,

int a[50][30] ; // automatic or static storage class
int (*b)[30] = malloc( sizeof( int[30] ) * 50 ) ; // dynamic storage class

note: b is really a pointer to the first element (which is an array of 30 integers).

Edited 3 Years Ago by mike_2000_17: Fixed formatting

Yes I agree, treating the array which I created as being contiguous would be a mistake. The code snippet was just meant to correct the OP's typo.

Thanks for your answers again.I understood it better.(with the help of drawing diagrams of memory on a paper...:) )

This article has been dead for over six months. Start a new discussion instead.