How to allocate 3d matrix dynamically in c programming where dimension will be [50][n][n] ; where n , I am taking through command line .
Asked
Active
Viewed 106 times
-1
-
1Does 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 Answers
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
-
i dont know why i am not able to modify array allocated with this.Value is not changing – Niraj singh Feb 22 '21 at 11:33
-
@Nirajsingh, please put your code and description what is wrong to the question – tstanisl Feb 22 '21 at 11:38