3

Is there a way to write the following function code

int* returnFilledArray() {
 int* arr = new int[2];
 arr[0] = 1;
 arr[1] = 2;
 return arr;
}

somehow like that? (create and fill the array as one liner).

int* returnFilledArray() {
 return new int {1,2};
}

if have tried various combinations, but I allways get some syntax errors, so when if it's possible I would be thankful for a detailed explanation.

Nicola Coretti
  • 2,655
  • 2
  • 20
  • 22
  • 1
    try this int[] returnFilledArray{......} – Badr Apr 13 '11 at 13:17
  • 3
    Be aware that if you create and return an array like that, there is no way to know how long it actually is, unless you have a known constant length for all arrays. I'd much rather recommend `std::vector` instead of a plain array. – Mephane Apr 13 '11 at 13:17
  • @Mephane: `std::vector` also has the advantage that you could use [Boost.Assign](http://boost.org/doc/libs/1_46_1/libs/assign/doc/index.html) (only needed pre-C++11). – Björn Pollex Apr 13 '11 at 13:26
  • @moon: `int[] returnFilledArray();` and `int* returnFilledArray()` are exactly the same function signature, so that comment (while upvoted) makes no sense at all. – David Rodríguez - dribeas Apr 13 '11 at 13:26
  • I can't use std::vector because we have to code a small compiler without using datastructures from the std. – Nicola Coretti Apr 13 '11 at 13:32
  • why it has to be a one liner? – Donotalo Apr 13 '11 at 13:38
  • @Nicoretti: Then about about a plain old C-like `struct int_array { int* data, int length };` and returning such a struct by value? – Mephane Apr 14 '11 at 07:23

7 Answers7

3

Yes..

std::vector<int> returnFilledArray()
{
  int a[] = {1, 2};
  return std::vector<int>(a, a+2);
}
Nim
  • 33,299
  • 2
  • 62
  • 101
1

C++ 0x supports initializer lists - is that an option for you?

Jeff Foster
  • 43,770
  • 11
  • 86
  • 103
1

I had answered similar question here. You can use following technique:

template<typename T, T _0, T _1, T _2, T _3, T _4> struct Array
{
  static T* Init (T *a)
  {
    a[0] = _0;
    a[1] = _1;
    a[2] = _2;
    a[3] = _3;
    a[4] = _4;
    return a;
  }
};

int* fun ()
{
  return Array<int, 1, 2, 3, 4, 5>::Init(new int[5]);
}
Community
  • 1
  • 1
iammilind
  • 68,093
  • 33
  • 169
  • 336
  • this is just an example for 5 elements; you can change the number of elements as per your need. Here the example is for `int`, but the same struct you can use for any type. – iammilind Apr 13 '11 at 13:32
  • So a simple version like in java: public static int[] myFunc() { return new int[] {2,3,4,19}; } is in c++ not possible? – Nicola Coretti Apr 13 '11 at 13:40
  • Not in current standarad, in C++0x the same feature as Java is introduced called "Initializer lists" where you can declare such arrays. Additionally, you can initialize variables when you declare them in `class' itself. – iammilind Apr 14 '11 at 02:19
1

You cannot do what you are trying to do in Standard C++ (03) without using special libraries.

John Dibling
  • 99,718
  • 31
  • 186
  • 324
0

I was able to do similar task using

int *ptr = new int[]{1, 2, 3};

you can then return from your function as

return (new int[]{1, 2, 3});
0

In C++0x:

#include <vector>
std::vector<int> returnFilledArray()
{
  return std::vector<int> {1,2} ;
}
TonyK
  • 16,761
  • 4
  • 37
  • 72
0

If you're not allowed to use std::vector, then you can try a macro:

#define FILLED_ARRAY(name) int name[] = {1, 2}
John Dibling
  • 99,718
  • 31
  • 186
  • 324
Donotalo
  • 12,748
  • 25
  • 83
  • 121