-1

How to allocate 3d matrix dynamically in c programming where dimension will be [50][n][n] ; where n , I am taking through command line .

  • 1
    Does this answer your question? [Dynamic memory allocation for 3D array](https://stackoverflow.com/questions/2438142/dynamic-memory-allocation-for-3d-array) – costaparas Feb 22 '21 at 10:17
  • Another duplicate covering any number of dimensions: [Correctly allocating multi-dimensional arrays](https://stackoverflow.com/questions/42094465/correctly-allocating-multi-dimensional-arrays). – Lundin Feb 22 '21 at 10:44
  • yes but value is not modifying after one row – Niraj singh Feb 22 '21 at 11:35
  • @Nirajsingh: which value is not being modified? Can you edit your question to show what you're trying to do? – John Bode Feb 22 '21 at 14:54
  • There was an bug i solved it..thank you – Niraj singh Feb 23 '21 at 15:14

1 Answers1

3

If your compiler supports C99 and beyond you can use automatic Variable Length Array (VLA):

int arr[50][n][n];

If n is expected to be large then you can allocate VLA on dynamic storage:

int (*arr)[n][n] = malloc(50 * sizeof *arr);

In both cases individual elements are accessed by arr[i][j][k].

tstanisl
  • 13,520
  • 2
  • 25
  • 40