+ 1
What is being passed?
When an array name is passed to a function, what is actually being passed?
4 Answers
+ 3
when you pass array to func, array will decay to a pointer holding the address of first element inside the array
+ 2
Lily Mea thanks đđđ»
+ 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;
}
+ 2
Flash okayy so we use pointers, thanks a lot for the help!đ€