Thread: Struct

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    79

    Struct

    Hello,

    I'm looking at struct and I'm not sure what the difference between the following code:

    Code:
       struct name {
          char *first;
          char *last;
       };
    and putting 'typedef' at the start and some name afterwards is.

    ie:

    Code:
       typeset struct {
          char *first;
          char *last;
       } name;
    Could someone please explain this to me?

    Thanks

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    With the former you would use struct name as the type name, but with the latter you would use name as the type name.
    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
    Mar 2010
    Posts
    79
    Um okay, which approach/style is better then?

    Thanks

  4. #4
    Registered User
    Join Date
    Apr 2009
    Posts
    145
    Personally i prefer using typedef before struct.........It gives more flexiblity. Lets see what others have to say about this?

  5. #5
    Registered User
    Join Date
    Jul 2010
    Posts
    26
    Quote Originally Posted by Mini View Post
    Um okay, which approach/style is better then?
    Typedefs save us some typing (I guess this makes the latter approach 'better') and are commonly used in projects involving structs.

  6. #6
    Registered User
    Join Date
    Mar 2010
    Posts
    79
    I've been trying to implement the first code into my function... where do I place it in the main function?

    Sorry for this elementary question, but after reading 5 different sets of notes, I still have no clue...

    Thanks

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Put your definition of the struct in global space, right under your include file listings. That way all your functions can declare a local struct instance, as needed (it's common).

    In main() declare the actual instance of the struct, itself, and a pointer to it. You'll probably want to pass around the pointer to it, to various functions. The advantage is that only functions that you pass the pointer to, will be able to change your struct (or array of structs if that's what you have instead of just one struct).

    And it's typedef, not typeset.

  8. #8
    Registered User
    Join Date
    Mar 2010
    Posts
    79
    Quote Originally Posted by Adak View Post
    In main() declare the actual instance of the struct, itself, and a pointer to it. You'll probably want to pass around the pointer to it, to various functions.
    Do I make a pointer to the whole struct (ie. 'myname') or to each individual 'component'?

    And it's typedef, not typeset.
    Thanks for that lol

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    To the whole struct. Then you can use the pointer to struct notation, that looks so fancy:

    Code:
    structPointerName->structMemberYouWant;
    Very snazzy!

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The point of a struct is to "group" variables together so you can pass them all together to where you need them. This is especially handy with many variables.
    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.

  11. #11
    Registered User
    Join Date
    Mar 2010
    Posts
    79
    Hmm okay... does anyone have a link to a good tutorial covering this?

    I'm not sure how to make a pointer to the struc...

    Thanks alot for helping me with these rather elementary questions.

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Cprogramming.com Tutorial: Structures
    Also, you might find SourceForge.net: Common mistakes and errors - cpwiki useful. Don't create pointers without initializing them with a valid storage location.
    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.

  13. #13
    Registered User
    Join Date
    Mar 2010
    Posts
    79
    Hello,

    Thanks for the tutorial but... here's my biggest area of misunderstanding... In the code:

    Code:
       struct name {
          char *first;
          char *last;
       };
    why are there dereferences? Whereas none of the notes and tutorials that I've read have it... this is the biggest area of my confusion atm

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    They are pointers. Do you understand pointers?
    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.

  15. #15
    Registered User
    Join Date
    Mar 2010
    Posts
    79
    Quote Originally Posted by Elysia View Post
    They are pointers. Do you understand pointers?
    Yes I know pointers... but pointers need to point somewhere... and I'm not sure where/how to point them to.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help me please.
    By yann in forum C Programming
    Replies: 15
    Last Post: 09-29-2009, 09:04 PM
  2. help with structs and malloc!
    By coni in forum C Programming
    Replies: 20
    Last Post: 09-14-2009, 05:38 PM
  3. Replies: 1
    Last Post: 12-03-2008, 03:10 AM
  4. Replies: 10
    Last Post: 05-18-2006, 11:23 PM
  5. What's wrong with my search program?
    By sherwi in forum C Programming
    Replies: 5
    Last Post: 04-28-2006, 09:57 AM