Struct

This is a discussion on Struct within the C Programming forums, part of the General Programming Boards category; Hello, I'm looking at struct and I'm not sure what the difference between the following code: Code: struct name { ...

  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
    21,987
    With the former you would use struct name as the type name, but with the latter you would use name as the type name.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    139
    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,820
    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,820
    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,820
    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.

Page 1 of 3 123 LastLast
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, 02: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

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