Arrays as function parameters in C

  Рет қаралды 12,365

CodeVault

CodeVault

Күн бұрын

Source code can be found here:
code-vault.net/lesson/a4mqix8...
===== Support us through our store =====
code-vault.net/shop
===== Check out our website =====
code-vault.net
===== Check out our Discord server =====
discord.code-vault.net

Пікірлер: 52
@MsHofmannsJut
@MsHofmannsJut 2 жыл бұрын
Just remember, that when you're accessing array elements, all array subscripts are sugar for pointer arithmetics. a[2] is sugar for *(a+2). b[size][2] is sugar for *( *(b+size) + 2 ). Then, of course, it makes sense why `arrParam[2][2]` does not work for pointer-decayed arrays: it's *( *(arrParam + 2) + 2 ), which in this case *( 3 + 2 ). and explains why arrParam[10] is ok: *( arrParam + 10 ) The additional subscript doesn't add a dimension to the pointer. This would make no sense. [x][y] multiplies the x dimension y times. Just like you would if you were drawing a table. You can initialise a multidimensional array like this: `arr[2][2] = { 1,2,3,4 };` and it will be the same as `arr[2][2] = {{ 1,2 }, { 3,4 }}` arr[0][2] == arr[1][0] == *( arr[0] + 2 ) == *( arr[1] ) == *( *arr + 2 )
@CodeVault
@CodeVault Жыл бұрын
That initialization example is a great one to understand this concept. Very nice explanation!
@nicolasedome3767
@nicolasedome3767 3 жыл бұрын
This is a fancy channel of C programming. I love this. Thanks for your effort.
@fifaham
@fifaham 4 ай бұрын
Excellent presentation of dealing with multi arrays outside the calling fucntion.
@thosewhowish2b693
@thosewhowish2b693 2 жыл бұрын
Wow, I never realized it was like so! I could swear using arrParam[0][0] inside the function would work. Thanks for this!
@zimablue2664
@zimablue2664 2 жыл бұрын
what do you think about this way: double a1[] = { 0.5,-1.0,-1.0 }; double a2[] = {-1.0, 1.0, 2.0 }; double a3[] = {-1.0, 2.0, 1.0 }; double *A[3]; A[0] = a1; A[1] = a2; A[2] = a3; if you pass A to a function as a double**, you have no problem at all
@shivaprasad761
@shivaprasad761 3 жыл бұрын
Love your videos 🙌
@tomaszstanislawski457
@tomaszstanislawski457 Жыл бұрын
The cast to `(int*)` can easily invoke UB since accessing 1-D array is allowed only for indices 0 to 5. The proper declaration of `printSize` would be `void printSize(int (*arrParam)[5])` or `void printSize(int arrParam[][5])`.
@SuperSamsosa
@SuperSamsosa Жыл бұрын
Top explanation. Seen others but yours is concise and good to understand. Thx
@torarinvik4920
@torarinvik4920 3 жыл бұрын
I love these low-level videos.
@KangJangkrik
@KangJangkrik 2 жыл бұрын
Thanks sir, you just saved me from another segmentation fault pain
@iAmTheWagon
@iAmTheWagon 2 жыл бұрын
Best teacher. My pain and anguish have been extinguished!
@ashishm8850
@ashishm8850 3 жыл бұрын
Simply superb!
@zltn_brkl
@zltn_brkl 3 жыл бұрын
You are the best always telling the gist.
@MIIQ130
@MIIQ130 3 жыл бұрын
thanks for the video
@divyanshuverma5652
@divyanshuverma5652 3 жыл бұрын
After many unsuccessful attempts, I finally understood the logic behind arrays in c and a good way of passing a 2d array in a function. Wow, amazing work dude.
@tomaszstanislawski457
@tomaszstanislawski457 Жыл бұрын
This is **not** a proper way to pass a 2d array. The proper one is `void foo(int rows, int cols, int arr[rows][cols])`, next the function will be called with `foo(5, 5, arr)`.
@wassimhaimoudi
@wassimhaimoudi 11 ай бұрын
@@tomaszstanislawski457he literally used this same way with the single array. More of a generalised way is to pass each dimension individually alongside the array. But that was not the point of the video. The video explains how we can visually interpret what happens in memory since all entities that we allegedly declare as arrays are nothing but a contiguous collection of values in memory that we can access them linearly through pointer arithmetics. The idea was clear at least in my opinion.
@tomaszstanislawski457
@tomaszstanislawski457 11 ай бұрын
@@wassimhaimoudiTechnically speaking accessing `int[5][5]` as `int[25]` for index 5 or larger is Undefined Behavior due to doing pointer arithmetic outside of the object. Only the first row of type `int[5]` can be accessed this way. IMO, using VLA typed parameters is a cleanest and most readable way of handling 2-d arrays.
@matteopisati9966
@matteopisati9966 2 жыл бұрын
Thank you
@jettgoldberg1015
@jettgoldberg1015 2 жыл бұрын
0:13 me whenever I learn anything related to coding LMAOOO Thank you for making this video! I didn't understand this concept at all in class and I need to learn it to do homework tonight
@MesbahSalekeen
@MesbahSalekeen 2 жыл бұрын
This channel is not for beginner level c tutorial. It's more like you learnt the basic c terms and trying to make a project and getting your concepts more clear.
@MesbahSalekeen
@MesbahSalekeen 2 жыл бұрын
Thst's more like it.
@CodeVault
@CodeVault 2 жыл бұрын
Well, there are tons of tutorials for absolute beginners out there but, what most people need is more intermediate level concepts. Especially when dealing with C. Those are the ones harder to grasp and difficult to explain to students
@escapefelicity2913
@escapefelicity2913 3 жыл бұрын
It might be a REAL GOOD IDEA to post the code
@CodeVault
@CodeVault 3 жыл бұрын
It's in the description ;)
@escapefelicity2913
@escapefelicity2913 3 жыл бұрын
Thank you
@MesbahSalekeen
@MesbahSalekeen 2 жыл бұрын
7:38 blew my mind
@yuuiichri
@yuuiichri 2 жыл бұрын
Thank you for the video! I just have one question, why do you have to convert the two dimensional array to an integer pointer in the main function before passing it to the function, I thought that an array becomes an integer pointer anyway when getting passed to a function?
@CodeVault
@CodeVault 2 жыл бұрын
That's true, you don't have to cast that parameter there. But since I was casting a 2d array to a pointer, the compiler would give me a warning which I didn't want
@yuuiichri
@yuuiichri 2 жыл бұрын
@@CodeVaultThank you!
@RAZE_187
@RAZE_187 2 жыл бұрын
my brain hurts
@tomerfata
@tomerfata 3 жыл бұрын
Can you also explain about dynamically allocated arrays as parameters?
@CodeVault
@CodeVault 3 жыл бұрын
Yeah, but those are just simple pointers
@Hevletica
@Hevletica 2 жыл бұрын
What about passing the length of the second dimension as an argument in a function definition?
@CodeVault
@CodeVault 2 жыл бұрын
Yes! That's usually how you have to pass any arrays as parameters
@itsmespiazzy9704
@itsmespiazzy9704 3 жыл бұрын
Hey, CodeVault. I'm having a hard time understanding this topic. In 11:37 you say if I dereference a 2D Array I'll get just the first integer of the matrix, but when I done that in my main() function actually what I get is an error, because it expects an variable of type array, with is true because matrix[0] have an array. I really don't get that part, why *matrix works differentelly in a function parameter and main.
@CodeVault
@CodeVault 3 жыл бұрын
Arrays have a lot of "syntactic sugar" around them. When you define an array: int arr[10][10]; This is actually just 100 integers one after the other in memory and nothing else. But when you pass it as double pointer to a function: fun(int** arr); This assumes you have an array of pointers to arrays of int. And the memory layout for that is completely different. So, when you have a 2D array you have to pass it as a single pointer and dereference it accordingly: void fun(int* arr) { arr[i * 10 + j]; // i-th row, j-th column } When it's in main, the compiler does that automatically for you: int arr[10][10]; arr[i][j]; // this is the same dereference as the one in the function
@testvb6417
@testvb6417 2 жыл бұрын
@@CodeVault love you
@muaadakmal511
@muaadakmal511 2 жыл бұрын
Sir , an array declared in heap using malloc is global or not, as it retains in memory even after function returns , so can we edit it in any other function without passing as argument instead simply calling inside the fun by the ptr name
@muaadakmal511
@muaadakmal511 2 жыл бұрын
plz reply @ earliest of ur convience
@CodeVault
@CodeVault 2 жыл бұрын
Well... it's accessible from anywhere. But you still need a variable in function you want to use it that contains the address to that array. Otherwise you cannot just access it if its pointer is stored in another function. void functiona() { int arr = malloc(sizeof(int) * 5); } void functionb() { arr[0] = 5; // ERROR: Undefined variable arr } int main() { functiona(); functionb(); return 0; } So the memory you allocated in functiona is still there BUT you don't have access to it in functionb because you also need a pointer to it. And that pointer is local to functiona right now
@muaadakmal511
@muaadakmal511 2 жыл бұрын
Thanks alot
@muaadakmal511
@muaadakmal511 2 жыл бұрын
Is there a way to identify the type of var like whether it is char* or char**
@muaadakmal511
@muaadakmal511 2 жыл бұрын
Like whethere ther exist something like typeof() operator in python
@krclinic
@krclinic Жыл бұрын
What about string array..?
@CodeVault
@CodeVault Жыл бұрын
It will behave the exact same but only on the base (double) pointer
Visual Studio Code for C/C++ on Linux (2021)
13:04
CodeVault
Рет қаралды 111 М.
How to use scanf with fgets
8:59
CodeVault
Рет қаралды 32 М.
Double Stacked Pizza @Lionfield @ChefRush
00:33
albert_cancook
Рет қаралды 116 МЛН
Jumping off balcony pulls her tooth! 🫣🦷
01:00
Justin Flom
Рет қаралды 27 МЛН
Sigma Kid Hair #funny #sigma #comedy
00:33
CRAZY GREAPA
Рет қаралды 40 МЛН
Dereferencing in C
10:29
CodeVault
Рет қаралды 10 М.
Difference between memmove and memcpy
9:15
CodeVault
Рет қаралды 24 М.
How to properly deal with dynamically allocated memory
13:44
CodeVault
Рет қаралды 9 М.
Why are function pointers useful?
6:43
CodeVault
Рет қаралды 29 М.
String In Char Array VS. Pointer To String Literal | C Programming Tutorial
9:58
Why use hexadecimal instead of decimal?
13:05
CodeVault
Рет қаралды 5 М.
What does fork() actually return?
7:13
CodeVault
Рет қаралды 4,2 М.
Global functions in multi-file projects in C
10:39
CodeVault
Рет қаралды 8 М.
Function Pointers | C Programming Tutorial
18:31
Portfolio Courses
Рет қаралды 58 М.
Double Stacked Pizza @Lionfield @ChefRush
00:33
albert_cancook
Рет қаралды 116 МЛН