Return A Dynamically Allocated 2D Array From A Function | C Programming Tutorial

preview_player
Показать описание
Рекомендации по теме
Комментарии
Автор

Exactly the help I needed. Explained very clearly. Thank you

battlelance
Автор

I had a very hard time to understand this concept when trying to change the *stack values when doing a DFS. I learned that, even if we have a *stack, we need to set the parameter of the function to change the *stack to **stack like:

void change_stack(stack_t **stack, int value) // This is a pointer to the stack pointer and is needed to change/access the stack itself by dereferencing it later.

void stack_pop(stack_t **stack) {
if(!*stack) {
printf("Stack is empty\n");
return;
}

stack_t *old = *stack;

*stack = (*stack)->next;
free(old);
}

Otherwise, if the function is void change_stack(stack *stack, int value), it does not change the actual value. We need the pointer to *stack.

I learned this the hard way ahah

Thank you for your videos. You're being a monumental help

MarcoAurelio-svtk
Автор

This is the best so far, i`ve found about arrays in functions, even better than college ones.

Gabriel-wqlw
Автор

this video should get more likes and views! Nice explanation!🙏

udarue
Автор

Thank you so much. I was confused on how to return an array (didn't know you could use pointers like this).

virivirus
Автор

Is it possible to write code that produces the same result using the `static` keyword?

mins
Автор

Thank you so much for this detailed explanation. I've been having trouble trying to understand the logic ✌️💪🙂

NachoDev
Автор

Another way is to create a single pointer, alocate all elements once and use some arithmetic to get correct a(i, j) element. In the end, free all elements once.

andredcavalcante
Автор

I would not advise allocating a 2D array like this because the multiple malloc calls fragment the array across memory, which has significant impacts on performance. Instead you should allocate one big array of [m*n] and then typecast the array to [m][n]

eruhinmakhtar
Автор

If I understand the subject correctly, this is not exactly a 2d array in C. The returned matrix functions as if it's a 2d array, but in fact, it is a 1d array of pointers, each pointer points to the first element of a 1d array. This is different compared to an actual 2d array created by the following code:
int a[2][3]; // where a will decay to a pointer to a whole 1d array of size 3, i.e., int (*ptr)[3];

TL-fesi
Автор

this is really helpful. Thank you so much!

nathanassefa
Автор

It's better to store matrix in an 1D array by rows or by columns (in fortran manner). See LAPACK interface, for instance.

qwertyqwerty
Автор

Weird, when an 100% identical "solution" as written in the main function, i mean code syntax, cannot be applied in a function below main. (*arrayPTR)[i] vs *(int*)arrayPTR + i).

AnalogDude_
Автор

why use malloc(sizeof(int*)*m) to allocate space for rows in 2d array ?

abdullah.sherdy
Автор

I understand from a "monkey see monkey do" point of view. However, I don't see how the syntax jives with what is happening under the hood. (1) To access any element A[i][k], the compiler just needs a pointer to the first element and the size of the array. Why does it need a pointer to a pointer. (2) Also, is it true that all that the compiler keeps track of is a single pointer for the first element of the array, rather than "m" pointers for each row of the array? Thanks.

karimshariff
Автор

Awesome vid, I tried making a snake game, but stuck on a seg fault lol taking a break for now.

juanmacias