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?
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.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
When you pass an array as an argument, it decays to a pointer to its first element, so you are actually passing a pointer.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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]) { }
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
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...
Last edited by anon; 05-02-2007 at 01:01 PM.
why pass array ?? as some said, you pass pointer to that array and then return the pointer
"C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do, it blows away your whole leg."-Bjarne Stroustrup
Nearing the end of finishing my 2D card game! I have to work on its 'manifesto' though <_<
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