Thread: Passing a structure to a function?

  1. #1
    Unregistered
    Guest

    Passing a structure to a function?

    I'm pretty new to C++.
    How would I go about making a structure that's global (I need to change members of the structure at any given moment), and send it to a function?

    For fun let's say I have a struct gun(this will be a function in my game)
    ----------------------------------------------
    struct gun
    {
    int damage,reload,capacity;
    char level;
    }
    //or something to that effect
    -----------------------------------------------

    Now, later in the game I have a battle funtion, and I have to send
    the function and all of it's members to that function.
    ------------------------------------------------
    //inside the void main()
    //...
    battle(struct gun);
    //...
    ------------------------------------------------
    Is that how I would do it? or not...

    And the real kicker is, at any given moment, someone might get another gun and I'd have to reassign all of the members of the structure. So, I'll have a function getNewGun.
    ------------------------------------------------
    getNewGun(struct &gun);
    ------------------------------------------------
    would that be it?
    and it would be a void function.
    Thanks for anyone who is willing to help me out!

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    65
    struct gun
    {
    int damage,reload,capacity;
    char level;
    } ;

    By using this you are creating a type not a variable. You should create a variable of this type and then send it to functions.

    So try these if you are not using global variables
    main()
    {
    struct gun mygun;
    battle(mygun);
    }

    void battle(struct gun &somegun)
    {
    sth
    }

    void getnewgun(struct gun &somegun)
    {
    sth
    }


    If you are using a global variable then

    struct gun mygun;

    main()
    {
    battle();
    getnewgun();
    }

    void battle()
    {
    sth
    }

    void getnewgun()
    {
    sth
    }

    You see you don't have to pass the global variable to functions. The mygun variable is visible to all functions automatically.

  3. #3
    Unregistered
    Guest
    It seems, quite obviously, that the best way to go is with global variables, but....erm....how do I go about doing this?
    (It's so much better in Java, just put them in the top )
    Thanks again

  4. #4
    Linguistic Engineer... doubleanti's Avatar
    Join Date
    Aug 2001
    Location
    CA
    Posts
    2,459
    you can still declare the instance of the struct in any global or local context...
    hasafraggin shizigishin oppashigger...

  5. #5
    Unregistered
    Guest
    is global the keyword?
    I don't know how to declare global variables.

  6. #6
    Registered User
    Join Date
    Nov 2001
    Posts
    53

    Ok

    Sounds like a class would be in order for most of these but anyway...

    The way you make 'mygun' available to a wider scope is to declare it in a difference place. What sort of scope do you want it to have? If you want it to be visible to all the function within your main source file you declare it BEFORE the main() function, like so -

    int myvar=4;
    int main() { ... ... ..
    ...
    ..

    If you want it to be visible to difference source files i believe you'll be wanting to declare it in a header file which will have to be included at the start of the code. To be honest though, if you're going to be doing lots of operations on it you really want to be thinking of using classes and making it private to the class etc. etc. (see the illustrious 'Hard to get Classes' thread )

    Hope this helps...

    Ah.
    "Technology has merely provided us with a more efficient means for going backwards" - Aldous Huxley

  7. #7
    Registered User
    Join Date
    Nov 2001
    Posts
    53
    btw.

    'global' is not a keyword - c++ kind of forces you to think about the scope..

    &

    you may notice that this isn't actually *so* different from Java - which may, perhaps, be accused of "borrowing" some of its functionality from a certain other language....

    Ah
    "Technology has merely provided us with a more efficient means for going backwards" - Aldous Huxley

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing a byte and its bits to a function
    By rtarbell in forum C Programming
    Replies: 9
    Last Post: 12-04-2008, 09:24 AM
  2. passing pointer to a structure in a function
    By steve1_rm in forum C Programming
    Replies: 5
    Last Post: 02-03-2008, 02:48 AM
  3. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  4. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 08:56 AM
  5. Serial Communications in C
    By ExDigit in forum Windows Programming
    Replies: 7
    Last Post: 01-09-2002, 10:52 AM