how to use typedefs with structs?

This is a discussion on how to use typedefs with structs? within the C Programming forums, part of the General Programming Boards category; Hey guys. Does anyone know how to use typedefs with structs, where they are held in a header file for ...

  1. #1
    Registered User
    Join Date
    May 2003
    Posts
    38

    how to use typedefs with structs?

    Hey guys. Does anyone know how to use typedefs with structs, where they are held in a header file for use in a multi-file linked list project?

    I am having trouble getting my head around it as the textbook doesnt give a lot of examples, and the assignment data types to be used contain errors in them.

    Here the data types:
    [code]
    #define MaxRaces 5;
    #define Maxname 30;

    typedef strcut{
    unsigned short Hours;
    unsinged short Minutes;
    unsigned short Seconds;
    } Time;

    typedef char Digit; /* 0..9*/
    typedef Digit Sail[ 3+1 ];
    typedef char Name[ MaxName + 1 ];

    struct Boat{
    Sail SailNum;
    Name BoatName;
    Time Duration[ MaxRaces ];
    };

    typedef struct Boat Boat; /*there s a Boat too much I think,but*/
    /*not entirely sure why */
    .
    .
    .
    .
    . /*there s some more of these data types but it d go on for*/
    /*a fair bit longer so I am not gonna post it*/

    If anyone could give me a hand in finding my direction I would very much appreciate.

    Thanks Johannes

  2. #2
    Registered User
    Join Date
    May 2003
    Posts
    38
    thanks for that. the 'strcut' was a typo. I can see what you mean but. Could the ';' have cuased a fair few compiler errors? because I had like a long list for my header file?
    Thanks,Johannes

  3. #3
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    Yes, the ';' will introduce a compiler error. Note that you use MaxRaces in the declaration of an array. So after substitution of the #define you get this:

    Time Duration [5;];

    Which will cause a compiler error.

  4. #4
    Registered User
    Join Date
    May 2003
    Posts
    38
    here s a few of the errors for the header file:

    Code:
    regatta.h:16: parse error before `;'
    regatta.h:21: parse error before `Name'
    regatta.h:21: warning: no semicolon at end of struct or union
    regatta.h:22: parse error before `;'
    regatta.h:28: parse error before `;'
    regatta.h:32: parse error before `Boat'
    regatta.h:32: warning: no semicolon at end of struct or union
    regatta.h:33: warning: data definition has no type or storage class
    regatta.h:34: parse error before `Place'
    regatta.h:34: warning: data definition has no type or storage class
    regatta.h:40: field `Racehistory' has incomplete type
    regatta.h:43: warning: useless keyword or type name in empty declaration
    Attached Files Attached Files

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    typedef Digit Sail[ 3+1 ];
    typedef char Name[ MaxName + 1 ];
    This is wrong. No, you're right, it actually does compile. But it's still wrong. You are declaring what exactly here? The first translates to:

    typedef char Sail[4];

    Which in your mind is doing what eactly? Also, since this is actually declaring a new data type, please illustrate how you'd create an instance of said type. It boggles my mind.

    For example, all of the following still allow my program to compile:

    Sail;
    Sail a;
    Sail b[5];

    And yet, I'm still not exactly sure what type of data you're trying to create or how you'd use these instances. It's just very very odd.

    Anyway, while it does compile, it's likely not behaving how you want it to.

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    May 2003
    Posts
    38
    Yeah I know. I havent had a chance to correct that problem yet. The file I attached was the original one that caused the errors. I ll get the changes done asap and see how I go. Appreciate the help. Cheers,
    Johannes

  7. #7
    Registered User
    Join Date
    May 2003
    Posts
    38

    typedef definitions and their structs

    That was exactly my problems. Those types have been given to us by our lecturer for the last assignments. We are to create a master file containing data for boats of different classes and then use linked lists to do different operations with their race results?!?
    Thanks for pointing that out but.
    Cheers,
    Johannes

  8. #8
    Registered User
    Join Date
    May 2003
    Posts
    38
    I sorted out the header problems. I had not included the 'tags'? for some of the typedefs, also some typo within the structs. I am still a bit shaky on the struct and typedef combination. Is it right that the 'tag' is what you can call the typedef struct by?
    like
    Code:
    typedef struct Boat{...} Boat;
    means that you have a 'variable' called Boat of type Boat?
    Cheers,Johannes

  9. #9
    Registered User
    Join Date
    May 2003
    Posts
    38
    > The red Boat is the structure tag name, which allows you to
    > create a variable using this notation
    > struct Boat ferry;

    so does that mean I create a struct of type Boat which I called ferry?

    > The green Boat is the typedef name, which allows you to create > a variable using this notation
    > Boat tanker;

    if it is the case above, what does this statement do than?

    so i need to use the red tag for pointers to point to that tag rather than to a struct?!?
    But if i do not need a pointer like *ptrNEXT, then the green tag is enough and I do not have to write "struct" before the tag all the time. Am I shooting in the correct direction or am I totally of the track?

    I am having a little trouble to visualise this.

    Thanks,
    Johannes
    Last edited by Yourhighness; 06-03-2003 at 04:27 AM.

  10. #10
    Registered User
    Join Date
    May 2003
    Posts
    38
    Thanks for that. What I meant with putting the 'struct' infront of it is when I have defined it once before. But thanks a lot I think I finally understood what this is all about, Johannes.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating array of structs
    By knirirr in forum C++ Programming
    Replies: 12
    Last Post: 06-18-2008, 08:30 AM
  2. Multidimentional structs + memcpy() == FAIL
    By Viper187 in forum C Programming
    Replies: 8
    Last Post: 06-18-2008, 02:46 AM
  3. packed structs
    By moi in forum C Programming
    Replies: 4
    Last Post: 08-20-2002, 01:46 PM
  4. ArrayLists + Inner Structs
    By ginoitalo in forum C# Programming
    Replies: 5
    Last Post: 05-09-2002, 05:09 AM
  5. Searching structs...
    By Sebastiani in forum C Programming
    Replies: 1
    Last Post: 08-25-2001, 12:38 PM

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