0

I'm new to C++ programming and i want to make a function that gives you all the common dividers into an array, and afterwards use it in int main()

Is returning an array from a function possible :D?

Thank you!

Code: http://pastebin.com/K8195wzF

il_guru
  • 8,383
  • 2
  • 42
  • 51
Samuel Cota
  • 57
  • 1
  • 2
  • 6
  • Probably a duplicate of http://stackoverflow.com/questions/3473438/c-return-array-in-a-function – Silas Mar 27 '13 at 15:03
  • 2
    Use an `std::vector` and return that. You can also use an `std::array` if you know the size at compile time. – juanchopanza Mar 27 '13 at 15:04
  • 2
    To start with, in C++ you should learn not to think about arrays, but about [`std::vector`](http://en.cppreference.com/w/cpp/container/vector). – Some programmer dude Mar 27 '13 at 15:06
  • You can return a pointer to the array. However finding the size is becomes an issue. Use `std::vector` instead. – andre Mar 27 '13 at 15:08

4 Answers4

4

Returning a dynamically-sized container like std::vector has the best use case here:

#include <vector>
...

std::vector<int> dividers(int x, int y) {
    std::vector<int> divi;

    ...
    return divi;
}

Returning by value creates a copy, ensuring no dangling pointers. Such is the case when returning a local array through a pointer: the memory will be lost when the function exits leaving the pointer to point to garbage.

David G
  • 94,763
  • 41
  • 167
  • 253
1

Yes it is possible to return an array in form of a pointer, but this is not recommended as you won't know its size in advance. But take care not to return the address of a local variable (allocated on the stack!!). Another problem with your code is that you didn't initialize d. The preferred C++ way would be to return a vector of the type (you do not need d anymore)

   #include <vector>
   ....
   vector<int> dividers(int y,int x){
    int yx;
    vector<int> divi;
    divi.reserve(100);
    if(y > x){
      yx = y;
    }
    if(x > y){
      yx = x;
    }

    for (int n = 1; n <= yx; n++){
      if ((n%y==0) && (n%x==0)){
        divi.push_back(n);
      }
    }

    return divi;
  }

You should read about iterators too to modify the loop that is in your main function.

Big mistake in my code removed... Thanks for pointing it out. I did the same mistake with vector that what you had with the array. Fixed now... Stupid me...

jdehaan
  • 19,700
  • 6
  • 57
  • 97
-1

No it's not possible. You could return a vector, or you could pass a pointer to the first element of an array to your function.

Looking at your code, the pointer option would look something like this

int dividers(int y,int x, int* divi){
    ...
}

int main()
{
    int divi[100];
    ...
    dividers(x, y, divi);
}

The vector solution might look like this

std::vector<int> dividers(int y,int x){
    std::vector<int> divi;
    ...
        divi.push_back(n); // when you want to add a divider to your vector
    ...
    return divi;
}

int main()
{
    std::vector<int> divi;
    ...
    divi = dividers(x, y);
}
john
  • 7,897
  • 29
  • 27
  • Not my downvotes, but fully deserved. The code suggested here contradicts best practices and there are good reasons for those, especially for answers to beginner questions. – Konrad Rudolph Mar 27 '13 at 15:09
  • Two of the downvotes appeared before the code did. Trouble with best practises is that they often take too long to explain to a newbie and they're not ready to absorb them. They'll learn them in good time. – john Mar 27 '13 at 15:10
  • “they’ll learn them in good time” is *provably* the wrong attitude to teaching. The best practice in this case is also the conceptually *easiest* solution. Teach that. – Konrad Rudolph Mar 27 '13 at 15:12
  • Konrad this isn't really a teaching site, it's a question and answer site. – john Mar 27 '13 at 15:13
  • I was simply responding to your point here. I also disagree: q&a is inherently teaching. If OP didn’t need to learn he wouldn’t have asked the question. – Konrad Rudolph Mar 27 '13 at 15:14
  • 1
    I'm guessing that the OP has never heard of vectors. There's a fair chance that any suggested solution using vectors will be ignored because of this. – john Mar 27 '13 at 15:14
  • 1
    I think many of the regulars here have trouble remembering how difficult most people find programming, and give answers which are beyond the person asking the question. Conceptually simple isn't really the point. – john Mar 27 '13 at 15:16
  • I totally agree with you on this – *and for that reason* I am very passionate about teaching them the conceptually simpler and cleaner solution to start with. Adding pointers makes the whole thing *vastly* more complex. `std::vector` can be safely treated as a blackbox and used as you’d expect (meaning you can just return the object etc.). The same is simply not true for pointers and C arrays, they behave very counter-intuitively and require lots of advanced knowledge to be treated correctly. – Konrad Rudolph Mar 27 '13 at 16:09
  • Well I agree with that. But often (maybe mostly) the posters here have been started off on pointers. – john Mar 27 '13 at 16:34
-1

You could do the following:

typedef int arrT[10]

arrT *func() {}

whereby one is defining arrT to be an alias of an array of 10 ints or

int (*func())[10]{}

whereby one is defining func when dereferenced to return an array of 10 ints.

A using declaration such that using arrT = int[10] is also possible.

As others have noted, returning a std::vector from a function containing the type desired is possible with a function declared as std::vector<int> func(){}.

Also, you could use another container std::array<> and forget built-in arrays altogether.

Reference:

C++ Primer; Lippman, Lajoie, Moo

Mushy
  • 2,535
  • 10
  • 33
  • 54
  • @john Highly legal and effective in my opinion. – Mushy Mar 27 '13 at 15:31
  • Tried it on one compiler (VC++ 2008) error message is 'error C2090: function returns array'. I always thought returning an array was illegal. Have things changed in C++11? – john Mar 27 '13 at 15:33
  • @john I corrected the first part of the solution to be `arrT *func() {}` so that a pointer is returned rather than an array directly. You are correct. – Mushy Mar 27 '13 at 15:37
  • 1
    So how are you going to return a pointer to an array? Either it's global, or it's dynamically allocated or it's local (which would be an error). I think this alternative is worse than the others. – john Mar 27 '13 at 15:43
  • @john Since a pointer is being returned and an array is always convertible to a pointer to the first element of the array, this answer works, cleanly, and efficiently. You may disagree but I referenced a text I have used to learn c++11. – Mushy Mar 27 '13 at 15:57
  • @Mushy You are confusing a lot of things here. You *cannot* convert an array to “pointer to array” and if you explicitly take the address of a local array then you get a dangling reference. So you need to dynamically allocate the storage and then you’ve gained nothing over just using a normal dynamic array. – Konrad Rudolph Mar 27 '13 at 16:14
  • @john you forgot two cases, one could be a static array in the scope of the function. The second could be return an argument. this is useful for functional-like programming when you want to chain calls. `func2(func1(arr1, arr2))` if all our functions return `void` because of `error C2090` we're in a pinch, and our freedom as programmers is damaged, which is unforgivable. I hope the pointer version of Mushy is valid. – v.oddou Apr 23 '15 at 03:19