Calling structs in functinos

This is a discussion on Calling structs in functinos within the C++ Programming forums, part of the General Programming Boards category; I'm working on a program right now that requires me to create a few structures and functions, and the functions ...

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    11

    Calling structs in functinos

    I'm working on a program right now that requires me to create a few structures and functions, and the functions must call the structures I've created. When I compile my program, I get various scope errors. My structs are defined in my main() routine. Do I have to move the structs outside of the main() routine to stop getting these scope errors, or can I just use:

    Code:
    typedef struct name
       {
       variables
       };
    in my main() routine?

    Thanks for reading.

  2. #2
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    Move the structure definition outside of main and you shouldn't have any problems :-)
    Code:
    typedef struct {
      int i;
    } test;
    
    void f(test t)
    {
      test d = t;
    }
    
    int main()
    {
      test t;
    
      f(t);
    }
    *Cela*

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    11
    Thanks for the help. I thought without the typedef I had to put struct in the function call. Thanks for clearing this up for me.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Stack issues when calling a COM library
    By notnot in forum C Programming
    Replies: 3
    Last Post: 06-01-2009, 03:12 AM
  2. Creating array of structs
    By knirirr in forum C++ Programming
    Replies: 12
    Last Post: 06-18-2008, 09:30 AM
  3. Multidimentional structs + memcpy() == FAIL
    By Viper187 in forum C Programming
    Replies: 8
    Last Post: 06-18-2008, 03:46 AM
  4. ArrayLists + Inner Structs
    By ginoitalo in forum C# Programming
    Replies: 5
    Last Post: 05-09-2002, 06:09 AM
  5. Searching structs...
    By Sebastiani in forum C Programming
    Replies: 1
    Last Post: 08-25-2001, 01:38 PM

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