passing struct variables

This is a discussion on passing struct variables within the C++ Programming forums, part of the General Programming Boards category; is there any way to pass a structure vairable to a procedure? i've tried but it doesnt seem to work. ...

  1. #1
    Registered User Guido's Avatar
    Join Date
    Apr 2004
    Posts
    14

    passing struct variables

    is there any way to pass a structure vairable to a procedure? i've tried but it doesnt seem to work. 44 errors =/

  2. #2
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Do you mean the entire structure or just a variable in the structure? Either way, you should be able to do it. Here is an example of passing a structure:

    Code:
    #include <iostream>
    
    struct MyStruct
    {
    int a;
    
    };
    
    int myFunc(MyStruct obj)
    {
    std::cout<<obj.a<<std::endl;
    
    }
    
    int main()
    {
    MyStruct MyObj;
    MyObj.a = 10;
    myFunc(MyObj);
    
    }
    Although if you want the function to modify its contents you should pass it as a pointer or a reference
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  3. #3
    Registered User Guido's Avatar
    Join Date
    Apr 2004
    Posts
    14
    i need the function to modify it, time to learn pointers. thanks.

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Or you could just pass it by reference.

    Using the above code:
    Code:
    int myFunc(MyStruct &obj)
    {
      obj.a = 100;
    }
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  5. #5
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Shh...wait till he learns pointers, then tell him that
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  6. #6
    Registered User Guido's Avatar
    Join Date
    Apr 2004
    Posts
    14
    do i have to declare the structure at the top? cause that would screw up my program a bit

  7. #7
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    You have to declare it before you use it. You can put it in a header if you want and include the header at the top in any source files you wish to use the structure in.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Struct question... difference between passing to function...
    By Sparrowhawk in forum C++ Programming
    Replies: 6
    Last Post: 02-23-2009, 03:59 PM
  2. Assignment HELP!!
    By cprogrammer22 in forum C Programming
    Replies: 35
    Last Post: 01-24-2009, 02:24 PM
  3. Replies: 1
    Last Post: 12-03-2008, 03:10 AM
  4. Acessing a struct after passing to a function
    By bomberto in forum C++ Programming
    Replies: 5
    Last Post: 10-04-2006, 05:29 PM
  5. Function validation.
    By Fhl in forum C Programming
    Replies: 10
    Last Post: 02-22-2006, 08:18 AM

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