Thread: Should I put a structure delcaration in a header file?

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    26

    Question Should I put a structure delcaration in a header file?

    Hi all:

    As a general rule for good programming practice,
    Should I put the following structure declaration in a header file or in a .c file?

    Code:
       typedef struct VIEW_TAG
    {
    	char name[16];
    	unsigned int field_Count;
    	VIEW_MEMBER *ptrViewMembers;
    	VIEW_MEMBER *ptrLastAddedMember;
    	struct VIEW_TAG *ptrNextView;
    }VIEW;
    Thank you

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What would your own answer be, and why?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    26
    ? Not so sure about it.

    Because when we include header files, it is simply a copy and paste action.
    So my guess is that it does not really matter but I heard a few times that header file should not include definitions at all.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Consider the case where you would like to use this struct in multiple source files. Would it not make more sense to put it in a header, and then include the header in each of the source files?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Sep 2008
    Posts
    26

    Smile

    Ennnnnnn.....



    That is right. I did not think that far. Thank you for your reply.

    Thank you

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  3. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  4. #include header files or .cpp files?
    By DoctorX in forum C++ Programming
    Replies: 3
    Last Post: 12-23-2006, 12:21 PM