Passing structs as parameters

This is a discussion on Passing structs as parameters within the C++ Programming forums, part of the General Programming Boards category; Hey guys, I have a struct which has its elements used in a function and then another function is called ...

  1. #1
    182
    182 is offline
    Registered User
    Join Date
    Oct 2005
    Posts
    133

    Passing structs as parameters

    Hey guys, I have a struct which has its elements used in a function and then another function is called which doesn't require the structs elements but then this function then calls a third function which does require them. At the moment i have the first function passing the structs object to the second function and then from the second to the third function even though the second function doesn't require the structs object data.

    Was this the best way to do this or is there another way? Thanks.

  2. #2
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    Use a pointer to the struct. When working with a struct through a pointer, don't forget to use the "->" operator for dereferencing and working with the elements.

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Hey guys, I have a struct which has its elements used in a function and then another function is called which doesn't require the structs elements but then this function then calls a third function
    It sounds like you are using the wrong program structure. main() should be the air traffic controller. main() should call the first function and send it the necessary arguments, and the first function should then return to main(). Then main() should call the second function and send it the necessary arguments, and then the second function should return to main(), etc.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    >> Was this the best way to do this or is there another way?

    Yes. This assumes that there is a reason the first function calls the second function which calls the third function. You say that "the second function doesn't require the structs object data", but if the second function has to call the third function, and the third function requires it, then the second function requires it.

    Chances are you could design your functions better, but without doing that your approach is correct.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newb Question on Passing Objects as Parameters
    By Mariano L Gappa in forum C++ Programming
    Replies: 12
    Last Post: 11-29-2006, 12:08 PM
  2. Replies: 7
    Last Post: 04-19-2006, 11:17 AM
  3. Question about passing & receiving parameters
    By TCB in forum C Programming
    Replies: 9
    Last Post: 04-11-2006, 06:08 AM
  4. Passing parameters to modal dialogs
    By Halloko in forum Windows Programming
    Replies: 2
    Last Post: 10-11-2005, 07:15 AM
  5. Passing parameters from VB to C++ through ActiveX DLL
    By torbjorn in forum Windows Programming
    Replies: 0
    Last Post: 12-10-2002, 02:13 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21