1

Can I return an array by value in C++? By value I mean returning a copy instead of using a pointer. If so can someone provide an example of returning a 1d array and a 2d array?

Xavier
  • 8,828
  • 13
  • 64
  • 98

3 Answers3

6

No; you can return arrays by value only by wrapping them inside a struct or a class (actually, you can simply return an appropriate instance of std::array, which encapsulates a C-style array inside a template class which provides also some bells and whistles).

Matteo Italia
  • 123,740
  • 17
  • 206
  • 299
  • `std::vector` would work as well (if the OP doesn't have TR1 or C++11 libraries) – Praetorian Nov 01 '11 at 01:47
  • @Praetorian: but it is a completely different thing (more powerful, but with some overhead and overkill for several tasks). – Matteo Italia Nov 01 '11 at 01:48
  • It is different, but it can do everything `std::array` can (and more as you've rightly mentioned). I was just listing an alternative in case the OP didn't have access to `std::array` – Praetorian Nov 01 '11 at 01:50
  • @Praetorian: sure, I was just pointing out that it's not *exactly* what he asked. :) – Matteo Italia Nov 01 '11 at 01:51
1

No this is impossible, unless you use some kind of custom array class (such as std::array as mentioned by Matteo Italia). Arrays in C/C++ are treated in much the same way as pointers.

jli
  • 6,523
  • 2
  • 29
  • 37
  • 5
    No. Arrays in C and C++ are ***not*** pointers. They *decay* to pointers in many occasions, but they are a different beast. Have a look at the [array FAQ](http://stackoverflow.com/questions/4810664/how-do-i-use-arrays-in-c/4810676). – Matteo Italia Nov 01 '11 at 01:44
  • How is it not effectively a pointer that is allocated automatically? If you could provide a link to a document concerning this it would be appreciated. – jli Nov 01 '11 at 01:47
  • You can read the C or C++ standard (where you'll see that arrays and pointers are separated types with different rules), or the aforementioned FAQ. – Matteo Italia Nov 01 '11 at 01:47
  • When your comment was first posted the link was not included. I do see that my initial comment about them being the exact same is wrong, however they behave in much the same way and in almost all cases can be treated as the same. – jli Nov 01 '11 at 01:54
  • @jli [this thread](http://stackoverflow.com/questions/4607128/in-c-are-arrays-pointers-or-used-as-pointers) (among plenty of others) contains discussion about the topic and parts of the standard that Matteo mentioned. – AusCBloke Nov 01 '11 at 01:55
  • @jli: correct, I added the link a minute later; if you read the FAQ, they often behave in the same way because often arrays decays to pointers and arrays declarations in function parameters are treated as pointer declarations, but you must take extra care because there are subtle ways in which they differ. – Matteo Italia Nov 01 '11 at 11:25
1

No this is not allowed in C/C++ .

You can return array by reference, if it's not local to function. The other work around is to wrap it inside a 'struct/class' wrapper and return its object.

iammilind
  • 68,093
  • 33
  • 169
  • 336