C Double dimensional arrays

How can double dimensional arrays be dynamically initialized in C ?

Showing Answers 1 - 17 of 17 Answers

dynamic initialization of double dimensional arrays:

int num[rows,cols] = {row1col1, row1col2, row1col3.......row2col1, row2col2, row2col3,..........row3col1, row3col2, row3col3, ..........rowncoln};

hope this is clear.

  Was this answer useful?  Yes

vasanth

  • Mar 1st, 2006
 

I think you told it for static declaration and not for dynamic. please make me clear between static and dynamic

  Was this answer useful?  Yes

Dharmarajan V .[Programmer]

  • Mar 2nd, 2006
 

    main()

   {

     int m,n;

     cin>>m>>n;

     int a[m][n];

    }

  Was this answer useful?  Yes

mansoor

  • Mar 7th, 2006
 

it is wrog...! beczuse u can not use cariable while definig array size..!!

it can be done by using malloc() function..! i just dont remember exact coding but it look like that.

int size;cin>>size

int *array;

array=malloc(sizeof(int)*size);

its is for single dimension array...but its not exact code for it..

  Was this answer useful?  Yes

saisreelu

  • Mar 8th, 2006
 

U can assign the memory dynamically for a 1 dimensional array in the following way:

int *a;

int i; //Can be any value

a = (int *)malloc (i * sizeof(int));

Dynamic allocation of arrays of more than one dimension is not so easily done, because dynamic allocation of an n-dimensional array actually requires dynamic allocation of n 1-dimensional arrays. To allocate a 2-dimensional array you must first allocate memory sufficient to hold all the elements of the array (the data), then allocate memory for pointers to each row of the array. For arrays of more than two dimensions it gets more complicated.

  Was this answer useful?  Yes

Richa Yadav

  • Mar 12th, 2006
 

#includeint **arr = malloc(rows * sizeof(int)); for (i=0; i

  Was this answer useful?  Yes

paulson paul chambakottukudyil

  • Apr 12th, 2006
 

If you use calloc(m,n) for allocating the memory, all of them will be initialized with zeros.

  Was this answer useful?  Yes

Soundari

  • Apr 25th, 2006
 

We can use the following code to allocate memory for a double dimensional array of R rows and C columns.

int ** ppIntArr = NULL;

ppIntArr = (int**) malloc(R * sizeof(int**));

for(int i = 0; i < R; i++)

{

ppIntArr[i] = (int*) malloc(C * sizeof(int*)); 

}

  Was this answer useful?  Yes

binnoor

  • Jul 20th, 2006
 

I think this can be done like that 

int ** p = 0;
 
 p = new int * [nRows];

 for(int i = 0 ; i < nRows ;i++)
 {
  p[i] = new int [nCols];
 }

// release memory

 for(i = 0 ; i < nRows ;i++)
 {
  delete [] p[i];

 }
 delete [] p;

Harsh Nanchahal

  • Aug 18th, 2006
 

It has been written in c++ that no array allocated dynamially be initialized.So there has to be a zero argument constructor for that class.

  Was this answer useful?  Yes

If the question is "How to initialize memory that is dynamically allocated for a 2D array", there are a couple different ways.

1. Use memset. memset sets a chunk of memory to a given value. Ultimately, 1D or 2D, memory is memory.
2. Run a two dimentional loop
    for(int i = 0; i < 10; i++)
         for( int j = 0; j < 20; arr[i][j++] = ' ');

I may be a little off with the syntax, but hope you get the idea.
                    

  Was this answer useful?  Yes

There are multiple ways to do this :
1. int **arr = (int**) malloc(nrows * sizeof(int*));
    for (i=0 ; i<nrows ; i++)
    {
       arr[i]=(int*) malloc (ncols * sizeof(int));
    }

2. int **arr = (int *) malloc (nrows*ncols*sizeof(int));

nrow are number of rows and nclos are number of columns.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions