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

  1. #16
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You are supposed to pass the variable spade, not spade[0,13] (is that even valid syntax?).

    I am getting a little confused by Java at the moment, but in C++ I would write it as:
    Code:
    void displayCards(string spade[])
    {
        for (int i=0; i<13; i++)
            cout << "S: " << spade[i];
    }
    
    // To call it.
    displayCards(spade);
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  2. #17
    Registered Abuser Loic's Avatar
    Join Date
    Mar 2007
    Location
    Sydney
    Posts
    115
    Quote Originally Posted by MacGyver View Post
    You're making up syntax here.

    What is this?

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

    Code:
    displayCards(spade);
    hahahaha yeah, i wasn't pt sure about that, but it compiled fine, and no one picked up on it so i thought i must of been right... but even after i fixed that up it still doesn't want to work for me! now i get
    19 expected `,' or `...' before "spade"
    In function `void displayCards(std::string*)':
    22 `spade' undeclared (first use this function)
    [Build Error] [main.o] Error 1

  3. #18
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Be careful of your case. In your early examples, you wrote it as Spade. Later, you wrote it as spade. You need to be consistent because C++ has case sensitive identifiers.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #19
    Registered Abuser Loic's Avatar
    Join Date
    Mar 2007
    Location
    Sydney
    Posts
    115
    Quote Originally Posted by laserlight View Post
    You are supposed to pass the variable spade, not spade[0,13] (is that even valid syntax?).

    I am getting a little confused by Java at the moment, but in C++ I would write it as:
    Code:
    void displayCards(string spade[])
    {
        for (int i=0; i<13; i++)
            cout << "S: " << spade[i];
    }
    
    // To call it.
    displayCards(spade);
    F@$k ME! after all that i just had to move the "[]" over, hahaha thanks for the help every one!

    the joys of learning, really dose your head in at times

  5. #20
    Registered Abuser Loic's Avatar
    Join Date
    Mar 2007
    Location
    Sydney
    Posts
    115
    I was just curious, you can pass an array to an function, but can a function return an array?

  6. #21
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Short answer is yes, but there's a few things to consider. When you pass an array to a function, you're really passing a pointer to the first element under the hood. You don't really see this, but it's vitality important to understanding how this works.

    The pointer/array relationship becomes a little more apparent when trying to return an array. You end up having to return a pointer to the first element of the array. All good, right?

    Well, here's another issue: You can't return local arrays. Local variables are destroyed some time around the end of the function because they are allocated on the stack. If you return a block of memory from a function, as a general rule you need to dynamically allocate it with new (or malloc() for C programs).

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