-5

My question is similar to this one, but not the same. That one is input array then return array. Mine is input integers, and return array.

I have an array that gets a few numbers, and makes it an array.

int makearr(int a, int b, int c){
    arr[2]={a,b,c};
    return?//how can I return it
}

int main(){
    //and use it?
}

All I want is to make the array, and display it.

Note: Sorry, my software doesn't support tuples.

Community
  • 1
  • 1
  • 1
    Use a `std::pair`. If you absolutely need an array, return a `std::array`. The language prohibits returning arrays from functions. – chris Jul 25 '14 at 04:35
  • That can only return two things. – user3875138 Jul 25 '14 at 04:36
  • An your function takes two things. In fact, your function could be replaced with `std::make_pair`, or probably just `{a, b}` depending on where you use it. – chris Jul 25 '14 at 04:37
  • Sorry, it's just an example. :) – user3875138 Jul 25 '14 at 04:38
  • pay attention, you try to return local variable. – folibis Jul 25 '14 at 04:42
  • Ok, depending on the usage, `{a, b, c}` or `std::make_tuple(a, b, c)` might be better alternatives. If an array is still the best, I suggest looking up an implementation of `make_array` so you call `make_array({a, b, c})` and get back a `std::array`. – chris Jul 25 '14 at 04:42
  • @folibis, By value. Returning things by value is completely safe. – chris Jul 25 '14 at 04:43
  • @chris With return-type being `std::array`, couldn't he just do `{{a,b,c,}}`? – Pradhan Jul 25 '14 at 04:45
  • If `std::array` and `std::tuple` are not available, there's always `boost::array` and `boost::tuple`. – chris Jul 25 '14 at 04:46
  • you may use std::vector to return. – Prasaathviki Jul 25 '14 at 04:47
  • @Pradhan, In the function, yes, but if you're going to be turning N `int`s into a `std::array`, you might as well have a function that takes a list of arbitrary length, except my mistake, you'd probably call it like `make_array(a, b, c);`. – chris Jul 25 '14 at 04:49
  • And yes, my suggestions are all over the map. I really have no idea what the use case of the function is, so I can't know what to focus on. For most use cases, not even calling this function and just using `{a, b, c}` is probably fine. – chris Jul 25 '14 at 04:52
  • I'm going to call the function multiple times. – user3875138 Jul 25 '14 at 04:53
  • 1
    @user3875138, All of these suggestions work multiple times. – chris Jul 25 '14 at 04:55
  • Oh! OK. Can you post an answer, I'm a bit confused. – user3875138 Jul 25 '14 at 04:56
  • @user3875138, I still don't know exactly what you want? Could you provide a description of why you need this function and where using it will be useful? – chris Jul 25 '14 at 04:57
  • Make the array and display it. – user3875138 Jul 25 '14 at 04:58
  • possible duplicate of [Return array in a function](http://stackoverflow.com/questions/3473438/return-array-in-a-function) – prince Jul 25 '14 at 04:59
  • @prince it's similar, but different situation. – user3875138 Jul 25 '14 at 05:06
  • How is it different? The fact that you put arguments in and they use a for loop makes no difference to the answer. – Flexo Jul 25 '14 at 07:08
  • @Flexo To someone who isn't all that experienced with C/++, this appears to be a slightly different situation. Some of the comments here are unique to this question and useful. – Zenexer Jul 25 '14 at 11:00

3 Answers3

4

A vector?

template <typename T>
std::vector<T> makearr (T a, T b, T c)
{
    T d[] = { a, b, c };
    return std::vector<T>(d, d + 3);
}

std::vector<int> x = makearr(1, 2, 3);
// access x[i]

If all manner of standard containers are verboten, then:

template <typename T> struct three { T d[3]; };

template <typename T>
three<T> makearr (T a, T b, T c)
{
    three<T> d = { { a, b, c } };
    return d;
}

three<int> x = makearr(1, 2, 3);
// access x.d[i]

Borrowing from here and here, I cobbled together this C++11 solution that doesn't use standard containers:

template <typename T, unsigned N>
struct argh {
    argh (std::initializer_list<T> i) {
        std::copy_n(i.begin(), std::min(i.size(), N), v_);
    }
    T & operator [] (int i) { return v_[i]; }
    const T & operator [] (int i) const { return v_[i]; }
private:
    T v_[N];
};

template <typename T, typename... Type>
auto makearr (T&& val, Type&&... vals) -> argh<T, 1 + sizeof...(Type)>
{
    argh<T, 1 + sizeof...(Type)> arr = { val, vals... };
    return arr;
}

auto x = makearr(1, 2, 3);
// access x[i]
Community
  • 1
  • 1
jxh
  • 69,070
  • 8
  • 110
  • 193
  • I'm so sorry, I'm not allowed to use vectors in this mission. – user3875138 Jul 25 '14 at 04:48
  • What are you allowed to use? – jxh Jul 25 '14 at 04:50
  • Not vectors, not anything like a linked-list or deque. – user3875138 Jul 25 '14 at 04:52
  • 4
    That doesn't make any sense. Exactly what are you allowed to use, don't give me a list of things that I can't use that you will just add to as I use them. – jxh Jul 25 '14 at 04:53
  • @user3875138, is the array size fixed at compile time? – R Sahu Jul 25 '14 at 05:08
  • Is it possible to not? – user3875138 Jul 25 '14 at 05:08
  • There is no typesafe way to provide such a function without using C++11 features (variadic template). But, I am guessing that is a no go, since you won't allow `vector`. – jxh Jul 25 '14 at 05:09
  • Oh, OK then. Does C++11 mean any version above 11? I have Code::Blocks 13.12 – user3875138 Jul 25 '14 at 05:14
  • 1
    C++11 refers to a version of the C++ standard that is published by the standardizing committee. – jxh Jul 25 '14 at 05:15
  • So does my version count? – user3875138 Jul 25 '14 at 05:21
  • @user3875138, CodeBlocks is just an IDE, not a compiler. At a guess, the compiler you're using does support C++11, at least to some degree. I think the MinGW version comes with GCC 4.7.2 or something, which supports a fair amount, and there's an option right in the compiler settings saying something like "use C++11". – chris Jul 25 '14 at 05:26
  • Ok, how do you check what compiler it is? – user3875138 Jul 25 '14 at 05:26
  • @jxh, You can still hack a typesafe version into C++98. For example, Boost has a C++98 tuple. At any rate, it's not really something normal people do. – chris Jul 25 '14 at 05:28
  • @chris: You can create a `tuple` object, but there is no real support to create a function to pack one together safely without a variadic template. A variadic function is not typesafe. – jxh Jul 25 '14 at 05:29
  • @chris: In any case, this seems silly that the OP wants to use C++11 features but not allowed to use standard containers. – jxh Jul 25 '14 at 05:31
  • @jxh, Agreed, but remember that tuples kind of require variadic templates. The variadic template is what you can hack in (via a series of `typename` parameters). It wouldn't be truly arbitrary, but it is reasonably possible. – chris Jul 25 '14 at 05:34
1

here nothing complex is required, you just need to return an address of your first array element to a pointer,which will ultimately display the array entries. Array must be declared globally so that it is known to ptr in main(). the code is wrriten below:

int arr[3];   //Declare it globally
int main()
{
int *ptr;
ptr=makearr(2,4,5);
for(int i=0;i<3;i++)
{
cout<<*ptr<<endl;
ptr++;
}
return 0;
}

int* makearr(int a,int b,int c)
{
arr[0]=a;arr[1]=b;arr[2]=c;
return(&arr[0])
}
nobalG
  • 4,544
  • 3
  • 34
  • 72
Vishesh
  • 308
  • 1
  • 9
-4

A pointer? You can store the integers in either an integer array or in an integer pointer. A function cannot return an array, however it can return a pointer.

int* makearr(int a, int b, int c){
int *array = new int[3];
array = &a;
array+1 = &b;
array+2 = &c;
return array;
}

int main(){
int* arr = makearr(1,2,3);
// Do something with the array. Access the elements by dereferencing the pointer variable
std::cout<<*arr<<" "<<*(arr+1);
delete arr;
return 0;
}

This code however has a potential memory leak if you do not delete the array pointer in your main(or wherever you invoke the makearr method from)

SCCC
  • 341
  • 3
  • 13
  • You want to use `array[0] = a;` – R Sahu Jul 25 '14 at 05:15
  • How do you display it? – user3875138 Jul 25 '14 at 05:16
  • And does it some what relate to arrays? – user3875138 Jul 25 '14 at 05:17
  • I recommend you start reading some basic material on pointers and how they work in C. All arrays can be interchangeably used with a pointer, regardless of type. – SCCC Jul 25 '14 at 05:19
  • You're using the wrong `delete`. And arrays cannot be interchanged with pointers. What about `for (int i = 0; i < sizeof(arr) / sizeof(*arr); ++i)` or `ArraySize(arr)` given an appropriate function template, or in `decltype(arr)` where the result should be of a specific type? – chris Jul 25 '14 at 05:19
  • R Sahu- `array[0] = a;` and `array=&a` do the same thing – SCCC Jul 25 '14 at 05:20
  • I take that as a YES? – user3875138 Jul 25 '14 at 05:20
  • 1
    No Sumit, they don't do the same thing. Did you even try to compile this? – Benjamin Lindley Jul 25 '14 at 05:21
  • @SumitChakraborty, `array[0] = a;` copies `a` to the location pointed to by `array`. `array = &a;` makes `array` point to `a` (leaking memory, and making the next two assignments that would have just done nothing into undefined behaviour). – chris Jul 25 '14 at 05:22
  • @chris: The next two assignments don't even compile, since `array+1` is not an L-value. – Benjamin Lindley Jul 25 '14 at 05:25
  • @BenjaminLindley, My bad, I guess I extrapolated on how you can assign to the result of a typical user-defined `operator+`. – chris Jul 25 '14 at 05:29
  • I can count at least 3 sources of undefined behaviour here and even if the code was correct as you intended it's still not good advice. – Flexo Jul 25 '14 at 07:11