Like Tree2Likes
  • 1 Post By stahta01
  • 1 Post By hk_mp5kpdw

Can we use structure written in a header in another header without calling header

This is a discussion on Can we use structure written in a header in another header without calling header within the C Programming forums, part of the General Programming Boards category; Hi All, Can we use structure or function or variable written in a header into another header without including name ...

  1. #1
    Registered User phanideepak007's Avatar
    Join Date
    Nov 2011
    Location
    Earth
    Posts
    7

    Can we use structure written in a header in another header without calling header

    Hi All,
    Can we use structure or function or variable written in a header into another header without including name of header of which we are going to use the struc or func or variable of that particular header

    for ex:
    file.h header has
    Code:
    struct database {   int id_number;   int age;   float salary; };
    And i also have a header data.h
    can i use the struct mentioned above into data.h without using
    #include<file.h> in data.h file. I think we cannot but i am in situation where a header uses another header without #include defination.
    could not understand why is that... Kindly help me out

    Thanks n Regards.

  2. #2
    Registered User
    Join Date
    Oct 2011
    Location
    Denmark
    Posts
    80
    What do you mean by "using a header without #include"? Normally if you try to use a struct database without including the file.h header your compiler should tell you that it does not know this structure.

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    2,581
    There are times with the second header only needs to know that database is a structure; in that case adding this line in the second header will work. IIRC, this is called a forward declaration.

    Code:
    struct database;
    Tim S.
    phanideepak007 likes this.

  4. #4
    Registered User phanideepak007's Avatar
    Join Date
    Nov 2011
    Location
    Earth
    Posts
    7
    Many Thanks ... !!! Will look into it...

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,801
    Technically... if the source file you are compiling includes file.h before data.h then it should work although it is not something I would recommend. Doing things that way forces a requirement that header files be included in a certain order, this is to be avoided.
    phanideepak007 likes this.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  6. #6
    Registered User phanideepak007's Avatar
    Join Date
    Nov 2011
    Location
    Earth
    Posts
    7
    Thanks a ton..!! Its all clear now ... That is what happening in source file ...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Structure in header won't declare.
    By CornJer in forum C++ Programming
    Replies: 13
    Last Post: 10-22-2006, 01:30 AM
  2. Replies: 30
    Last Post: 06-19-2006, 12:35 AM
  3. Replies: 4
    Last Post: 12-14-2005, 01:21 PM
  4. error with 2 header files calling each other
    By paperbox005 in forum C++ Programming
    Replies: 3
    Last Post: 10-06-2004, 02:36 AM
  5. include library header in header files
    By Raison in forum C++ Programming
    Replies: 6
    Last Post: 09-27-2004, 02:50 AM

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