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.

7
  • 1
    try this int[] returnFilledArray{......} Commented Apr 13, 2011 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. Commented Apr 13, 2011 at 13:17
  • @Mephane: std::vector also has the advantage that you could use Boost.Assign (only needed pre-C++11). Commented Apr 13, 2011 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. Commented Apr 13, 2011 at 13:26
  • I can't use std::vector because we have to code a small compiler without using datastructures from the std. Commented Apr 13, 2011 at 13:32

7 Answers 7

3

Yes..

std::vector<int> returnFilledArray()
{
  int a[] = {1, 2};
  return std::vector<int>(a, a+2);
}
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

1

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

1 Comment

I just wanted to know if it is possible because you can do something like that in Java. I do not need it necessarily.
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]);
}

3 Comments

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.
So a simple version like in java: public static int[] myFunc() { return new int[] {2,3,4,19}; } is in c++ not possible?
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.
0

In C++0x:

#include <vector>
std::vector<int> returnFilledArray()
{
  return std::vector<int> {1,2} ;
}

1 Comment

He said he can't use a vector
0

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

#define FILLED_ARRAY(name) int name[] = {1, 2}

5 Comments

-1: he wants a dynamically allocated array. this one is automatic, and will fall off the stack.
i wasn't sure if he REALLY wants the array to be on heap. or just wants a one liner.
Well, I guess I'm not sure either, so I edited your post and reversed the d/v
@John, thank you. but what is the edit!? i don't see any change.
All I did was add a space at the end. I couldn't reverse my d/v because it had been there for 15 minutes. So I just edited it myself with an empty edit. :)
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});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.