Arrays as Function Parameters in C programming

preview_player
Показать описание
Discover how to use arrays as parameters in C programming functions. You will be guided through the process by this lesson, which also explains why arrays are supplied by reference and demonstrates how to read and change array components inside of methods. Recognize the significance of array size and know how to use it appropriately. Learn this fundamental idea to create reliable and effective C programs.

👨🏻‍🏫 This complete tutorial is compiled by Sandeep Soni, a Microsoft Certified Trainer, a Software & Corporate Trainer for 27years!

📲 𝗖𝗼𝗻𝗻𝗲𝗰𝘁 𝘄𝗶𝘁𝗵 𝘁𝗿𝗮𝗶𝗻𝗲𝗿👋
Call Sandeep Soni for Career Guidance ► +91 98490 01840

👍 𝗗𝗼𝗻'𝘁 𝗙𝗼𝗿𝗴𝗲𝘁 𝘁𝗼
- Like the video if you found it helpful.
- Hit the bell icon to get notified of our latest videos.

💬 Join the Conversation
Have questions or need further clarification? Drop a comment below, and we'll be happy to help!

🌐 𝗙𝗼𝗹𝗹𝗼𝘄 𝗨𝘀:

🔔 Subscribe for more
Stay tuned for more in-depth tutorials and DevOps content to help you ace your certification exams and excel in your career.

#deccansoft #bestitcourses #sandeepsoni
Рекомендации по теме
Комментарии
Автор

amazing.... you are too good sir...studies full semester couldn't understand...understood in 17 mins...

vivekshanbhag
Автор

hi, i have an array variable like this $data = $myarray[0]); and need to pass this to a function mmr($array, $output = 'mean') ; it works if i manually make a test array like this :
arr = Array(1020, 30, 37, 9); and then i can echo it out echo '.($arr, 'mean') by passing $arr but trying to pass the array variable instead does not work! I need to get the variable from some loop in my code so need to pass that array which is already filled not a manual one

sparkden
Автор


What does this mean? Say you pass an array into this function:

void print_numbers(int array[5]){ for(int i = 0; i < (sizeof array / sizeof *array); i++) printf("%i\n", array[i]); }

What do you think will happen?

Oh, heheh, no, it's not gonna print the array. It's going to commit *U N D E F I N E D B E H A V I O R*. On my system, it prints out exactly two numbers from the array since sizeof array = 8 (a 64-bit pointer) and sizeof *array = 4 (a 32-bit int). Now, what do you think would happen if the type of the array was char[]? Oops, 8 / 1 = 8, so it would go and print out 4 characters (including the null terminator) that it shouldn't be printing. Bam! There's your UB. And what if it's a single-element array? int array[1]? I just compiled it and got "1, 32767" as a result, when the array's contents are: {1}. So by using an array argument, I caused UB, and it resulted in the program printing out garbage.

Garbage. Exactly what the above code is. Exactly what the inaccurate information in this tutorial is.

Not only that, but the array type here provides no additional type safety -- you can STILL perform pointer arithmetic on an array function parameter. It truly is not an array. It is a pointer. This kind of declaration might be somewhat useful in C++, which has references, but C is not C++. There is no such thing as pass-by-reference in C.

*So don't declare an array argument.* They will always decay into a pointer, and all you will succeed in doing is confusing others. Be clear and declare it as a pointer: `int *array`

BradenBest