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

4
  • Probably a duplicate of stackoverflow.com/questions/3473438/… Commented Mar 27, 2013 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. Commented Mar 27, 2013 at 15:04
  • 2
    To start with, in C++ you should learn not to think about arrays, but about std::vector. Commented Mar 27, 2013 at 15:06
  • You can return a pointer to the array. However finding the size is becomes an issue. Use std::vector instead. Commented Mar 27, 2013 at 15:08

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Or more likely than a copy, a move :)
Thank you all for your replies, i guess i should learn more about declaring and using a dinamic array ;|...
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...

Comments

-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);
}

9 Comments

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.
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.
“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.
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.
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.
|
-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

7 Comments

@john Highly legal and effective in my opinion.
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 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.
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 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.
|

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.