Passing arrays to functions is a simple task can get really confusing when the compiler tries to change the code for you or just doesn't behave the way you expect. Check out our Discord server: / discord
That's taken care of by the OS. It is allocated in more or less the same way you'd do it by calling a function yourself. As for where in memory it's stored... it depends, here's an answer from others that already did this research: stackoverflow.com/questions/26416678/where-does-the-os-store-argv-and-argc-when-a-child-process-is-executed
Yea, because if you don't want to modify you have to make a copy of it. If you pass an array to a function: int testFunction(int arr[]) { ... } That arr identifier would be treated as a pointer. So it won't ever automatically make a copy of your array on the stack. MAYBE, if you want it to be automatically copied you could have a struct that stores the array and pass that struct directly to that function: typedef structArray { int arr[50]; } structArray; int testFunction(structArray arr) { ... } That should copy the array automatically I think.
If it's a single dimensions array, you should pass it as int* and change it like any other array, if it's a multi-dimensional array then it's more complicated. There's this video on the topic: code-vault.net/lesson/a4mqix89a0:1610303947019
Pointers in disguise arr[n] is actually just a way to write *(arr +n). Thats why you can be cursed and do stuff like 3[arr] and it still works. Its just pointer math
OMG I FINALLY UNDERSTAND, THIS CONCEPT CAN BE SO HARD AND YOU MADE IT SO MUCH EASIER
"You, my friend, are a POINTER." Love it!!!
Thanks for explaining! Was looking for this for a while
this was exactly what i was looking for, thankyou so much for explaining this to me
Beautifully explained.
You are amazing! Thank you for the understanding!
you just point(put) your finger on the things that mostly matter. ARRAY POINTER. thank you for your effort
Awesome video, thank you!
it was helpful thank you.please upload more video about pointers and hard concepts in c/c++.
I love your videos please release more sir🙏
Thx God. You help me! Love 🙏🏻❤
from the function the receive array as a pointer, how do u get the size or length of the array since it returns the 8 bytes
You can't. You have to store it separately.
How do we create an Array of functions i.e an arrray consisting of functions parameters.
is this possible?
You mean a function pointer array?:
void fun1() { }
void fun2() { }
void fun3() { }
void (*function_array[3])() = {fun1, fun2, fun3};
What about the argv array passed in the main function? Where does that point?
That's taken care of by the OS. It is allocated in more or less the same way you'd do it by calling a function yourself. As for where in memory it's stored... it depends, here's an answer from others that already did this research: stackoverflow.com/questions/26416678/where-does-the-os-store-argv-and-argc-when-a-child-process-is-executed
Thanks! Great video btw
thanks
and if i don't want to update the array values what should i do??
You could pass a copy then
@@CodeVault i pass the array, and the inside the function i copy it to a new array. But i mean thats the unique solution?
Yea, because if you don't want to modify you have to make a copy of it. If you pass an array to a function:
int testFunction(int arr[]) { ... }
That arr identifier would be treated as a pointer. So it won't ever automatically make a copy of your array on the stack.
MAYBE, if you want it to be automatically copied you could have a struct that stores the array and pass that struct directly to that function:
typedef structArray { int arr[50]; } structArray;
int testFunction(structArray arr) { ... }
That should copy the array automatically I think.
@@CodeVault that could be a good solution. i will try, thanks
How to change values of arrays in a function when they are passed like this - int** arr?
If it's a single dimensions array, you should pass it as int* and change it like any other array, if it's a multi-dimensional array then it's more complicated. There's this video on the topic: code-vault.net/lesson/a4mqix89a0:1610303947019
Pointers in disguise
arr[n] is actually just a way to write *(arr +n). Thats why you can be cursed and do stuff like 3[arr] and it still works. Its just pointer math
I have an exam tomorrow, so best spent 5 minutes!
what about multidimensional arrays? 8)
There is this video on the topic:
code-vault.net/lesson/zgpd1zov1t:1603733527939