passing structures to functions

This is a discussion on passing structures to functions within the C++ Programming forums, part of the General Programming Boards category; I am currently having trouble passing structures to functions. Here is a portion of my code where Select_File is the ...

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    88

    passing structures to functions

    I am currently having trouble passing structures to functions.
    Here is a portion of my code where Select_File is the function and
    I am trying to pass the struct TXT to the function for editing.

    Code:
    typedef struct
    {
    int position
    int chars 
    int newlines
    }TXT;
    
    void Select_File(TXT *file_info );
    
    int main(void)
    {
    
    TXT *file_info;
    
    Select_File( file_info );
    }
    
    void Select_File( TXT *file_info )
    {
    
    
    }
    I am sure whatever I am doing wrong is clear to someone else, so if anyone is willing to offer help I would be much appreciative.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Don't forget your semicolons:
    Code:
    typedef struct
    {
    int position;
    int chars;
    int newlines;
    }TXT;
    My best code is written with the delete key.

  3. #3
    Even death may die... Dante Shamest's Avatar
    Join Date
    Apr 2003
    Location
    Malaysia
    Posts
    970
    Code:
    int main(void)
    {
    
    TXT *file_info;
    
    Select_File( file_info );
    }
    Minor warning: You seem to be actually passing pointers to structures. Be warned that file_info isn't pointing to any valid TXT object yet.

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    88
    so I should just replace file_info with TXT where the function call begins

    like this?

    Select_File( TXT )

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >so I should just replace file_info with TXT where the function call begins
    Dante's point is that file_info, while being a valid TXT pointer, doesn't actually point to any memory that you can use yet. If you're going to use it only in that function and then discard it you're fine. If you're going to point it to something and expect to use it after the function returns then you'll need to do a little more work. One easy solution is to just grab some memory in main and let Select_File fill it with useful stuff:
    Code:
    int main()
    {
      TXT *file_info = new TXT;
    
      Select_File(file_info);
      // Use file_info
    
      delete file_info;
    }
    Or create an object of typ TXT and pass the address of that object:
    Code:
    int main()
    {
      TXT file_info;
    
      Select_File(&file_info);
    }
    Or use a pointer and point it to an object of type TXT and pass the pointer, that way you still have file_info as a pointer in main and you don't have to worry about memory management.
    My best code is written with the delete key.

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    88
    Thank you all for all of your help. Especially Sandra

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-03-2008, 11:31 AM
  2. Help Understanding Passing Arrays To Functions
    By jrahhali in forum C++ Programming
    Replies: 7
    Last Post: 04-10-2004, 02:57 PM
  3. passing array structures to functions
    By lukejack in forum C Programming
    Replies: 2
    Last Post: 04-08-2003, 02:17 PM
  4. Passing structures between functions
    By TankCDR in forum C++ Programming
    Replies: 2
    Last Post: 01-29-2002, 09:54 AM
  5. Replies: 1
    Last Post: 01-20-2002, 10:50 AM

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