Thread: Structure Declaration

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    61

    Structure Declaration

    Hi There,

    I have a .h file that is accessed by two different .c file

    in my .h file I need a structure such as

    Code:
    struct {
    int mph;
    int distance;
    int numberofpeople;
    } car[8];
    and in both .c files, for example
    Code:
    car[0].mph = 100;
    how do i define the strcture,

    currently i keep getting an error of
    "multiply-defined:......"

    thanks

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,665
    In the .h file, you need
    Code:
    struct cartype {
    int mph;
    int distance;
    int numberofpeople;
    } 
    
    extern struct cartype car[8];
    In ONE of the .c files, you have
    struct cartype car[8];
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    The reason you get the error is that the header file declares an array of structs that becomes included in both source files. Both source files have a struct cartype car[8] array. When you try to link, the linker complains that you have this same array declared in two different source files. The struct definition needs to be visible to both source files but the actual declaration of the array can only be in one of those source files. This is what Salem's post above addresses.

    The extern declaration lets both source files know that there is an array that exists (somewhere, as Salem mentions it should only be in one of the source files) but does not actually cause any space to get allocated in either source file when the header is included. This allows the source file that does not actually contain the true array declaration know about it and properly take it into account when compiling.

    PS, I think Salem missed a semicolon at the end of the struct definition.
    "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

  4. #4
    Registered User
    Join Date
    Nov 2011
    Posts
    61
    hi guys,

    thanks for the responses, got the problem solved but also taught me how and why,
    thanks again

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Structure Declaration
    By lolaaa in forum C Programming
    Replies: 2
    Last Post: 10-10-2012, 02:18 PM
  2. Structure declaration
    By rk211083 in forum C Programming
    Replies: 18
    Last Post: 06-23-2010, 02:52 AM
  3. int s: 3 ; declaration in structure is possible
    By nkrao123@gmail. in forum C Programming
    Replies: 3
    Last Post: 12-17-2009, 06:17 AM
  4. Can you use a structure in the declaration of itself?
    By ititrx in forum C++ Programming
    Replies: 23
    Last Post: 11-07-2006, 03:40 PM
  5. structure declaration
    By Roaring_Tiger in forum C Programming
    Replies: 1
    Last Post: 03-26-2003, 11:22 PM