Thread: declaring a structure in another headerfile

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    25

    declaring a structure in another headerfile


  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I'm not sure what your question is, but declarations of structures are identical whether they are in a header-file or not. Your image is a bit hard to read on my machine, because of the dark brown text being hard to read against a black background - I expect you have less room -light than I do.

    You need to include the appropriate header-file in any other file that uses the structure [or include it in a header-file before the header-file that uses the structure, but that's less flexible and puts more strain on the programmer to make sure all header files are always in the right order - correctly written header files should only allow themselves to be included once by using include-guards].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Location
    Pencilvania
    Posts
    2
    A good practice to get used to is, like matsp already mentioned, using include guards. This makes sure that the include file is only included once. Like so:

    #ifndef FILENAME_H
    #define FILENAME_H

    /* definitions and declarations go here. */

    #endif /* FILENAME_H */
    Ofcourse you would replace FILENAME with the actual name of the include file.

  4. #4
    Registered User
    Join Date
    Mar 2008
    Posts
    3
    sorry only had a minute to post the thread.. i did a print scree and pasted it in paint, probably why its so dark..

    so anyway, i've declared the filename as:

    #include<netinet/ip> //ip.h which contains the structure "struct ip" and its contents "struct in_addr ip_dst ip_src" i am after.

    im after the method to declare the structure ip or has it already been declared?

    i want to access the contents so would it be like this?

    main()
    {
    ip *ip_hdr;

    or

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    When you declare an instance of a structure in C (C++ is different), you have to use the struct keyword.
    Code:
    struct s_t {
        int x;
    };
    
    struct s_t thestruct;
    Unless typedef or some such is used, then you need not.
    Code:
    typedef struct {
        int x;
    } s_t;
    
    s_t thestruct;
    Since there's no typedef in your structure definition, you'll need the "struct" keyword.

    Are you sure you're ready for socket/networking programming? It can be tougher than it looks, and if you don't know how to declare structures . . . .

    Perhaps you should look at these.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Note that if you don't know, main should return int, so it shouldn't be "main", it should instead be "int main."
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Mar 2008
    Posts
    3
    well its for my assignment that im working on. yes im rusty on my c, yes i don't know everything..

    so far i've been able to retrieve the mac address(source and destination), type.

    .. i know how to declare structures:
    http://img355.imageshack.us/my.php?image=structjg3.jpg
    and access them:
    http://img210.imageshack.us/my.php?image=struct2bw1.jpg

    im JUST wondering if its any different or not. when the structure is in the headerfile how do i declare the structure. just in the main function? or above even though the members have already been made in the headerfile.

    i haven't had time to fiddle around a lot yet.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It's not any different. Just include the header file and use it as normal.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem referencing structure elements by pointer
    By trillianjedi in forum C Programming
    Replies: 19
    Last Post: 06-13-2008, 05:46 PM
  2. Replies: 5
    Last Post: 02-14-2006, 09:04 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Serial Communications in C
    By ExDigit in forum Windows Programming
    Replies: 7
    Last Post: 01-09-2002, 10:52 AM
  5. C structure within structure problem, need help
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 11-30-2001, 05:48 PM