Hi
http://www.informit.com/guides/conte...eqNum=207&rl=1 here it writes:
"Functions in C/C++ can't return or receive array arguments"
Is this true?
Can't we pass array to function as value? Is it always pointer?
Printable View
Hi
http://www.informit.com/guides/conte...eqNum=207&rl=1 here it writes:
"Functions in C/C++ can't return or receive array arguments"
Is this true?
Can't we pass array to function as value? Is it always pointer?
That's normally true, yes.
Arrays are not passed by value, always as a pointer (or maybe as a reference in C++)
But if you embed your array inside a struct, then that would be passed by value, if you passed the containing struct by value.
When you pass an array as an argument, it decays to a pointer to its first element, so you are actually passing a pointer.
Thank you.
Here there is an information about passing array with value to func.
http://www.informit.com/guides/conte...eqNum=241&rl=1
Is this right?Code:template <typename T, size_t size>
void foo(T (&array)[size])
{
//sort array, find the sum, min, max, etc
}
Can we use like this? I mean is this really pasing array to func by value?
Seems right. If I understand templates right, size needs to be a compile-time constant, though.
This demonstrates the same thing. It may be easier to understand.Code:void foo(int (&array)[size]) {
}
Does it? I get size is undefined? The trick with the template is that size is a compile time constant.
Your approach would need magic constants both in the function and in the calling code, or the use of a global constant, define or something like that.
I'll probably need to check the link, because I don't get why and where this would be useful...
Edit: back. This is just a proof of concept...
why pass array :) ?? as some said, you pass pointer to that array and then return the pointer :)
Well, the aim is that size is deducted from the passed array, so you don't need to pass it separately.
However, this could be an example of "clever code", or a clever way to shoot yourself in the leg (the array has already decayed to pointer).
Any C++ programmer would consider standard containers first :)