How To Dynamically Allocate a 2D Array in C

preview_player
Показать описание
How To Dynamically Allocate a 2D Array in C: This is something I must have had to look up 100 times. in my opinion this is the best, most simple way to do it.
Рекомендации по теме
Комментарии
Автор

I was literally searching for this kind of tutorial from days. You’re a god send❤️

mohammadusmansiddiqi
Автор

You are a blessing - you were so clear and to the point thank you!

zacharygarbaty
Автор

Thank you soooo much! The way you present it makes the seemingly difficult content quite easily understood.

wenjieyu
Автор

you are awesome dude thx for the video it's so helpfull for me.

endercelik
Автор

You could allocate 2d array with a help of a pointer to an array. Just do `int (*array)[m] = malloc(n * sizeof *array);`.

tomaszstanislawski
Автор

Can u please tell how to do the same thing but inside a struct so I can control this object like a master of puppets ?

denisrabotay
Автор

You say array[2] is the "2nd column" ---> shouldn't this be the "3rd row" ??

mikehagerty
Автор

this is actually a very inefficient way to have 2D array in C,
this is an array of array pointers, not a 2D array.
this have a lot of disavantage, as it is not a 2D array but an array of array, function that take a 2D array as a parameter don't work with this kind of array.
also for a N * M array you have N + 1 memory allocation and deallocation, also to copy the array you need loops ect which is inefficient if you could copy the whole array at once.
the best way is to allocate everything together,
for an array of int of N * M the syntax would be "int (*arr)[M] = malloc(sizeof(int) * N * M);"
This is a weird syntax, but it applied for a lot of stuff in more complicated C, like function pointers
you start with the variable name, arr, and you go from right to left
(*arr) mean this is a pointer,
[M] mean this is pointer to an array of size M
then int, this mean this a pointer to an array of int of size M
so if you understand how pointers works, if you dereference arr, you obtain an array of size M.
this have a lot of advantages, everything is contigus in memory, so its faster, you don't need to have multiples pointers, you only need 1 malloc and 1 free.
and if you make a function that have a prototype like "void foo(int arr[][M]);" it will accept static 2D array but also 2D dynamic array.
this work for the number of dimension you want, N * M * 0 would be "int (*arr)[N][M] = malloc(sizeof(int) * N * M * 0));"
wich is a lot easier to read than an array of array of array, with a nested allocation.

Baptistetriple
Автор

free (array); just before "return 0" it could be builded on another fonction too so the work is done properly...

cherrabimohammed
Автор

how come not array = (int **) malloc(sizeof(int*) * n);

samsmith
Автор

Your program is a major memory leak. You did not free() the memory allocated with malloc()!

bryankreinhart
Автор

i trust you but still output is necessary...

shadestorm
Автор

Good video, even though it's lacking the explanation. Oh, and you actually filled the third collumn of the matrix, not the second one...

davinonnenmacher