2d array programming problem

Hi
i want to use a 2 dimensional array . i want to know that .Can i declare 2d array as?

int *a[3];
can anyone tell me how to use this kind of deceleration

how to put value in this array? how to print values of array ?

1
2
3
4
5
6
7
8
9
10
11
12
for(int i = 0; i < 3; i++)
    {
        a[i] = new int[3];
    }
    
    for(int vertical = 0; vertical < 3; vertical++)
    {
        for(int horizontal = 0; horizontal < 3; horizontal++)
        {
            a[vertical][horizontal] = (2+vertical)*(horizontal+3);
            cout << a[vertical][horizontal] << endl;
...


That makes *a[3] a new 2d array of ints a[3][3]. Remember to delete a.
For an array created like this:

1
2
3
4
5
6
int *a[3];

for(int i = 0; i < 3; i++)
{
   a[i] = new int[3];
}


Proper deletion would be like this:

1
2
3
4
for(int i = 0; i < 3; i++)
{
   delete [] a[i];
}
Topic archived. No new replies allowed.