Arrays as function parameters in C

preview_player
Показать описание
Source code can be found here:

===== Support us through our store =====

===== Check out our website =====

===== Check out our Discord server =====
Рекомендации по теме
Комментарии
Автор

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 )

MsHofmannsJut
Автор

This is a fancy channel of C programming. I love this. Thanks for your effort.

nicolasedome
Автор

Wow, I never realized it was like so! I could swear using arrParam[0][0] inside the function would work. Thanks for this!

thosewhowishb
Автор

Excellent presentation of dealing with multi arrays outside the calling fucntion.

fifaham
Автор

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.

divyanshuverma
Автор

Best teacher. My pain and anguish have been extinguished!

iAmTheWagon
Автор

Thanks sir, you just saved me from another segmentation fault pain

KangJangkrik
Автор

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])`.

tomaszstanislawski
Автор

Top explanation. Seen others but yours is concise and good to understand. Thx

SuperSamsosa
Автор

You are the best always telling the gist.

zltn_brkl
Автор

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

zimablue
Автор

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

jettgoldberg
Автор

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
Автор

It might be a REAL GOOD IDEA to post the code

escapefelicity
Автор

Can you also explain about dynamically allocated arrays as parameters?

tomerfata
Автор

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.

itsmespiazzy
Автор

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?

yuui
Автор

What about passing the length of the second dimension as an argument in a function definition?

Hevletica
Автор

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

muaadakmal
Автор

I think you can just do something like... without the cast

void printArr(int *arrArg) {
printf(" arg[0][2] : %d", (arrArg[0]+2));
}

int main(void) {
int arr[][5] = {
{1, 2, 3, 4, 5},
{6, 7, 8, 9, 10},


};
printArr(arr);
}

stack.