Thread: Is it possible to pass an array to a function?

  1. #1
    Registered Abuser Loic's Avatar
    Join Date
    Mar 2007
    Location
    Sydney
    Posts
    115

    Question Is it possible to pass an array to a function?

    Is it possible to pass an array to a function?

    For example, I am trying to make a function called displaycards, which will display some cards, but I want it to display the cards from a deck which I have in an array in my “int main()” part of the program.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Yes.

    Can you post the code related to your attempt?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered Abuser Loic's Avatar
    Join Date
    Mar 2007
    Location
    Sydney
    Posts
    115
    Some thing like this...
    Code:
    #include <cstdlib>
    #include <iostream>
    
    using namespace std;
    
    void displayCards();
    
    int main(int argc, char *argv[])
    {
        string Spade[13] = {"A", "2", "3", "4", "5" ,"6", "7", "8", "9", "10", "J", "Q", "K"};
        random_shuffle(Spade, Spade + 13);
            
        cout << "Player 1: \n";
        displayCards();
        
    }
    
    void displayCards()
    {
         for (int i=0; i<=13; i++)
         { cout << "S: "; spade[i]); }
    }

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    The function displayCards should receive arguments -- generally speaking, the start of the array and its size. And C and C++ are case sensitive.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Registered Abuser Loic's Avatar
    Join Date
    Mar 2007
    Location
    Sydney
    Posts
    115
    Quote Originally Posted by Dave_Sinkula View Post
    The function displayCards should receive arguments -- generally speaking, the start of the array and its size. And C and C++ are case sensitive.
    Sorry you lost me?

  6. #6
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    basically, edit the function (and the prototype, of course) so that it takes two arguments: an array of type string, and an integer, to specify the length of the array.

    hope it helps.

  7. #7
    Registered Abuser Loic's Avatar
    Join Date
    Mar 2007
    Location
    Sydney
    Posts
    115
    Quote Originally Posted by nadroj View Post
    basically, edit the function (and the prototype, of course) so that it takes two arguments: an array of type string, and an integer, to specify the length of the array.

    hope it helps.
    something like this?
    Code:
    #include <cstdlib>
    #include <iostream>
    
    using namespace std;
    
    void displayCards(string, int);
    
    int main(int argc, char *argv[])
    {
        string Spade[13] = {"A", "2", "3", "4", "5" ,"6", "7", "8", "9", "10", "J", "Q", "K"};
        random_shuffle(Spade, Spade + 13);
            
        cout << "Player 1: \n";
        displayCards(Spade, 13);
        
    }
    
    void displayCards(string Spade, int i)
    {
         for (int i=0; i<=13; i++)
         cout << "S: "; Spade[i]);
    }

  8. #8
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    almost.. with that code the function takes in a single string.. you want a string array.

  9. #9
    Registered Abuser Loic's Avatar
    Join Date
    Mar 2007
    Location
    Sydney
    Posts
    115
    Quote Originally Posted by nadroj View Post
    almost.. with that code the function takes in a single string.. you want a string array.
    Ok, so i have this.
    Code:
    void displayCards(string array, int);
    
    int main(int argc, char *argv[])
    {
        string Spade[13] = {"A", "2", "3", "4", "5" ,"6", "7", "8", "9", "10", "J", "Q", "K"};
        random_shuffle(Spade, Spade + 13);
            
        cout << "Player 1: \n";
        displayCards(Spade, 13);
        
    }
    
    void displayCards(string array Spade, int i)
    {
         for (int i=0; i<=13; i++)
         cout << "S: "; Spade[i]);
    }
    but i get this error? " conversion from `std::string*' to non-scalar type `std::string' requested "

    sorry if i am being a pain, i tried to look all this up on google, but that just confused me even more... lol

  10. #10
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    you know how to declare a string array, you did it in main! whats this 'array' keyword nonsense!

    KISS! its no different!

    edit: also, in your cout statement in the displayCards function remove the ')' and change the middle ';' to '<<'
    Last edited by nadroj; 05-06-2007 at 08:32 PM.

  11. #11
    Registered Abuser Loic's Avatar
    Join Date
    Mar 2007
    Location
    Sydney
    Posts
    115
    Quote Originally Posted by nadroj View Post
    you know how to declare a string array, you did it in main! whats this 'array' keyword nonsense!

    KISS! its no different!

    edit: also, in your cout statement in the displayCards function remove the ')' and change the middle ';' to '<<'
    ok, so so far i have this

    Code:
    #include <cstdlib>
    #include <iostream>
    
    using namespace std;
    
    void displayCards(string Spade);
    
    int main(int argc, char *argv[])
    {
        string Spade[13] = {"A", "2", "3", "4", "5" ,"6", "7", "8", "9", "10", "J", "Q", "K"};
        random_shuffle(Spade, Spade + 13);
            
        cout << "Player 1: \n";
        displayCards(Spade[0,13]);
        system("pause");
        return EXIT_SUCCESS;
    }
    
    void displayCards(string Spade)
    {
         for (int i=0; i<=13; i++)
         {
             cout << "S: " << Spade[i];
         }
    }
    it compiles fine, but when i run it i get this.
    This application has requested the Runtime to terminate it in an unusual way.
    Please contact the application's support team for more information.

  12. #12
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    ok, i think ive tortured you enough...

    to declare your array of strings, you did:
    Code:
    string Spade[13]
    string[] is the datatype--that is, its a 'string array'. in your function and prototype, append '[]' to the datatype:
    Code:
    void displayCards(string[] Spade);
    also, i would recommend using lower-case variable names (ie 'spade' not 'Spade') and camel-casing when necessary, as you have ('displayCards'). when i see a capital i think its a class name. my two cents.

  13. #13
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Even
    Code:
    void displayCards(string Spade[13]);
    would work. It's the same, you are declaring an array of strings, its just that this one is also a parameter to the function.

    One other issue, you should be using < instead of <=. When i = 13, it is out of bounds of a 13 element array (valid indexes are 0 - 12).

  14. #14
    Registered Abuser Loic's Avatar
    Join Date
    Mar 2007
    Location
    Sydney
    Posts
    115
    Quote Originally Posted by nadroj View Post
    ok, i think ive tortured you enough...

    to declare your array of strings, you did:
    Code:
    string Spade[13]
    string[] is the datatype--that is, its a 'string array'. in your function and prototype, append '[]' to the datatype:
    Code:
    void displayCards(string[] Spade);
    also, i would recommend using lower-case variable names (ie 'spade' not 'Spade') and camel-casing when necessary, as you have ('displayCards'). when i see a capital i think its a class name. my two cents.
    ok now i am really ripping my hair out!!!!

    Code:
    void displayCards(string[]);
    
    int main(int argc, char *argv[])
    {
        string spade[13] = {"A", "2", "3", "4", "5" ,"6", "7", "8", "9", "10", "J", "Q", "K"};
        random_shuffle(spade, spade + 13);
            
        cout << "Player 1: \n";
        displayCards(spade[0,13]);
        system("pause");
        return EXIT_SUCCESS;
    }
    
    void displayCards(string[13] spade)
    {
         for (int i=0; i<13; i++)
             cout << "S: " << spade[i];
    }
    i get this message from the compiler
    cannot convert `std::string' to `std::string*' for argument `1' to `void displayCards(std::string*)'

  15. #15
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    You're making up syntax here.

    What is this?

    Code:
    displayCards(spade[0,13]);
    Should be this:

    Code:
    displayCards(spade);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. how can i pass this array to my function?
    By kosodo in forum C Programming
    Replies: 4
    Last Post: 04-14-2006, 09:40 PM
  3. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. pass the pointer of a two dimension array to a function
    By SoFarAway in forum C Programming
    Replies: 8
    Last Post: 04-13-2005, 05:43 AM