Thread: which is the best way

  1. #1
    Registered User
    Join Date
    Apr 2019
    Posts
    808

    which is the best way

    i have a struct array called players. as it is an array is there any difference in
    Code:
    void foo(Hand players[])
    //or
    void foo(Hand *p_players)
    since my understanding is that if you pass an array to a function your actually passing a pointer to the array anyway.

    second one is if i have a function that needs 2 of the 6 members of the struct is it better to pass the two members separately or the entire struct ie.
    Code:
    foo(players[i].name, players[i].bets);
    //or
    foo(players)
    many thanks
    coop

  2. #2
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    In my humble opinion I would use the [ ]. The reason is that it gives the reader of the code a clue that an array is intended as the argument.

  3. #3
    Registered User catacombs's Avatar
    Join Date
    May 2019
    Location
    /home/
    Posts
    81
    Quote Originally Posted by Click_here View Post
    In my humble opinion I would use the [ ]. The reason is that it gives the reader of the code a clue that an array is intended as the argument.
    Agreed. Using the bracket notation in the parameter helps me remember I'm working with an array.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by cooper1200
    second one is if i have a function that needs 2 of the 6 members of the struct is it better to pass the two members separately or the entire struct
    That depends on the purpose of the function: does it do something to an object of the struct type, or does it do something to two objects that happen to match the members of your struct type?
    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

  5. #5
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    it just uses info thats held in the strut like name and placed bets etc

Popular pages Recent additions subscribe to a feed

Tags for this Thread