What is being passed? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

What is being passed?

When an array name is passed to a function, what is actually being passed?

8th Dec 2020, 2:23 PM
🏐Volley
🏐Volley - avatar
4 Answers
+ 3
when you pass array to func, array will decay to a pointer holding the address of first element inside the array
8th Dec 2020, 2:30 PM
durian
durian - avatar
+ 2
Lily Mea thanks 😊👍🏻
8th Dec 2020, 2:33 PM
🏐Volley
🏐Volley - avatar
+ 2
🏐Volley #include <iostream> // pass by ref void p(int (&r)[3]) { for (size_t i = 0; i < sizeof(r)/sizeof(r[0]); i++) { std::cout << r[i] << ' '; } std::cout << '\n'; } // pass by ptr void p(int* r, size_t len) { for (size_t i = 0; i < len; i++) { std::cout << r[i] << ' '; } std::cout << '\n'; } int main() { int m[] = { 1, 2, 3 }; p(m); // ref p(m, 3); // ptr return 0; }
8th Dec 2020, 2:50 PM
Flash
+ 2
Flash okayy so we use pointers, thanks a lot for the help!🤓
8th Dec 2020, 2:52 PM
🏐Volley
🏐Volley - avatar