How can I create a 2D dynamic array using malloc() in C? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

How can I create a 2D dynamic array using malloc() in C?

I want to create a program that multiply two matrices which dimensions are two variables (i and j). I can't understand how to use malloc() to allocate the right amount of memory and how to use it. I am developing using Visual Studio 2015.

3rd Dec 2016, 11:37 AM
Lorenzo Felletti
3 Answers
+ 2
int **arr = (int **)malloc(row * sizeof(int *)); for (i=0; i<row; i++) arr[i] = (int *)malloc(column * sizeof(int)); for more details check out: http://www.geeksforgeeks.org/dynamically-allocate-2d-array-c/
4th Dec 2016, 3:20 AM
Nikunj Arora
Nikunj Arora - avatar
+ 1
no problem 😀
5th Dec 2016, 11:44 AM
Nikunj Arora
Nikunj Arora - avatar
0
Thank you very much Nikunj Arora! 😉
5th Dec 2016, 11:13 AM
Lorenzo Felletti