0

Hi guys I'm trying to make a function to cut a card game stored in a array in half, I just would like to know if I can return two different array with my function

string* couperJeu(int idJoueur, string tab[52]){
    string jeuJoueur1[26];
    string jeuJoueur2[26];

    if (idJoueur == 0){
        for (int i = 0; i < 26; i++) {
            jeuJoueur1[i] = tab[i];
        }
    }
    if (idJoueur == 1){
        for (int y = 26; y < 52; y++) {
            jeuJoueur2[y] = tab[y];
        }
    }
    return(jeuJoueur1, jeuJoueur2);
}
12
  • 7
    A hint; no matter what your native language, always program in English. Commented Mar 19, 2017 at 17:17
  • 2
    The arrays go out of scope. So no. Instead use a vector and return a pair of vectors Commented Mar 19, 2017 at 17:17
  • 3
    First of all, read about std::array (or std::vector). Secondly, read about std::pair. Commented Mar 19, 2017 at 17:19
  • 1
    By the way, the second for loop will access jeuJoueur2 out of bounds Commented Mar 19, 2017 at 17:19
  • 3
    And also: Why do you want to return 2 arrays here? Your function only ever fills one of them with values Commented Mar 19, 2017 at 17:20

5 Answers 5

2

You can, if you use std::array instead of raw arrays:

std::array<std::array<std::string, 26>, 2> couperJeu(int idJoueur, string tab[52]) {
    std::array<std::array<std::string, 26>, 2> jeuJoueur;

    // fill jeuJoueur[0]/jeuJoueur[1] instead of jeuJoueur1/jeuJoueur2

    return jeuJoueur;
}

but you shouldn't. You only fill either jeuJoueur1 or jeuJoueur2, never both. Therefore, there is never a reason to return both.

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

Comments

0

No, you can't. In c++ isn't possible to return two arguments from function.

You can try use for example std::pair<> or define a struct to return two arguments

Comments

0

Another approach is to pass a reference to the function as a parameter, or in C-style code, a pointer. The function can then modify the object by reference, and the changes will be visible to the caller. This has the advantages that updating the array in place is much more efficient than making a deep copy, and that you can update as many objects as you want without the function having to manage memory.

On the other hand, returning a temporary vector or string object (not a reference to a temporary, which is illegal) will get optimized to make a more efficient shallow copy. You could also force this behavior for other kinds of arguments by wrapping them in a smart shared_ptr or unique_ptr. And making your function return information through its return values, and not side-effects, is a more functional style that is easier to debug and analyze.

Comments

0

Since this is a C++ question, you should use C++ data types, like std::vector, which will simplify your code.

const int DECKSIZE = 52;
std::pair<std::vector<card>, std::vector<card> > cut(
                int idPlayer, std::vector<card> arr)
{
    std::vector<card> a;
    std::vector<card> b;

    if (idPlayer == 0)
        for (int i = 0; i < DECKSIZE / 2; ++i)
            a.push_back(arr[i]);

    if (idPlayer == 1)
        for (int i = DECKSIZE / 2; i < DECKSIZE; ++i)
            b.push_back(arr[i]);

    return std::make_pair(a, b);
}

Notice the use of DECKSIZE instead of the magic numbers 52 and 26. This improves code readability.

But the code you suggest doesn't really make sense--if you are populating one of the arrays, why would you return both?

http://en.cppreference.com/w/cpp/utility/pair/make_pair

Comments

-1

Another way to do what operation you need to do is to make the two arrays global arrays and then create a void function to do all operations needed. Of course you can use vectors instead of arrays, but i recommend arrays if you are new user of c++.

7 Comments

Why would you recommend the (arguably) unsafer method for newer users? std::vector and std::array are more idiomatic modern C++ and safer to use
Yes, but a new c++ user is unfamiliar with vectors in my opinion. Of course, vectors are more powerfull and need no control from the programmer (I agree from this perspective with you).
New programmers are also unfamiliar with raw arrays and learning how to use std::vector or std::array takes about the same effort (probably even less because it removes many common causes of mistakes)
Generally I agree with you sir, but a new user in my opinion has to learn first the "c" way in order to understand better c++ , which is a better c . My opinion always .
C++ is not a "better" C - they are two separate languages that share a common ancestry but have evolved differently. There is no need to learn C to write C++ code (in fact a lot of C code doesn't even compile in C++).
|

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.