Structures -Help

This is a discussion on Structures -Help within the C Programming forums, part of the General Programming Boards category; On this page: C Tutorial – structures, unions, typedef | CodingUnit Programming Tutorials Why do they have the structure like ...

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    69

    Structures -Help

    On this page: C Tutorial – structures, unions, typedef | CodingUnit Programming Tutorials

    Why do they have the structure like this:
    Code:
        typedef struct telephone
        {
            char *name;
            int number;
        }TELEPHONE;
    When they could have left out 'telephone'. E.g.

    Code:
        typedef struct //Minus the 'telephone'
        {
            char *name;
            int number;
        }TELEPHONE;
    Thank you for any help given, this has been ........ing me off for a while.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    5,889
    The first form defines a type named "struct telephone", and TELEPHONE as a pseudonym for that.

    The second form makes TELEPHONE a pseudonym for an anonymous struct type.

    Practically, the difference is usually one of coding style and preference. Some people prefer to declare instances of the structure using "struct telephone a_phone" so they are explicitly documenting that a_phone is a data structure, not some other type.
    Right 98% of the time, and don't care about the other 3%.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,981
    Quote Originally Posted by inu11byte
    Why do they have the structure like this:
    If you wanted to have a pointer of that struct type as a member of the struct, then you need struct telephone, not just an anoymous struct. So, this might be a matter of consistency, or it might even be as simple as a matter of copy/paste or habit.

    By the way...
    The name of the type definition of a structure is usually in uppercase letters.
    Although there are instances of such a style being used (FILE comes to mind), generally the convention is to reserve fully capitalised names for macros and constants.
    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

  4. #4
    Registered User
    Join Date
    Dec 2011
    Posts
    69
    How come if I remove the 'typedef', I have to change the 'TELEPHONE' to lower-case? E.g.

    Code:
    //#includes
    	struct telephone
    	{
    		char *name;
    		int number;
    	}TELEPHONE;
    
    
    	int main()
    	{
    		struct telephone index;
    		struct telephone index2;
    //rest of code.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
     struct telephone
        {
            char *name;
            int number;
        }TELEPHONE;
    This defines a structure of type struct telephone, and an instance of that called TELEPHONE.
    Code:
            struct telephone index;
            struct telephone index2;
    Those make two more instances of struct telephone called index2 and index.


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

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,981
    Quote Originally Posted by inu11byte
    How come if I remove the 'typedef', I have to change the 'TELEPHONE' to lower-case?
    Because you wrote struct telephone instead of struct TELEPHONE. By the way, since you did not remove the TELEPHONE from after the struct definition, you ended up with a variable named TELEPHONE of the struct type.
    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

  7. #7
    Registered User
    Join Date
    Dec 2011
    Posts
    69
    Quote Originally Posted by quzah View Post
    Code:
     struct telephone
        {
            char *name;
            int number;
        }TELEPHONE;
    This defines a structure of type struct telephone, and an instance of that called TELEPHONE.
    Code:
            struct telephone index;
            struct telephone index2;
    Those make two more instances of struct telephone called index2 and index.


    Quzah.

    Okay, now I understand it. Thank you everyone for the help. I could have done it without properly understand how/why things are the way they are, but that's not the programmer way. Plus, it would have annoyed me.

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    5,889
    Quote Originally Posted by inu11byte View Post
    I could have done it without properly understand how/why things are the way they are, but that's not the programmer way.
    I applaud your sentiment here (I prefer to properly understand things where I can too). However, unfortunately, doing something without proper understanding is the "programmer way" all too often.
    Right 98% of the time, and don't care about the other 3%.

  9. #9
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,490
    Quote Originally Posted by grumpy View Post
    However, unfortunately, doing something without proper understanding is the "programmer way" all too often.
    Isn't that what abstractions are all about? (To not have to understand the boilerplate details of something ?)
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  10. #10
    Registered User
    Join Date
    Jun 2005
    Posts
    5,889
    Quote Originally Posted by manasij7479 View Post
    Isn't that what abstractions are all about? (To not have to understand the boilerplate details of something ?)
    Not precisely. Abstractions are alternative ways of thinking about particular aspects of a problem, and solutions to it. You are right that abstractions can be used to shield you from lower level details. But you still need to understand the abstractions, even if you don't understand the low level details that underpin them.

    As evidence of my original point: how many times in this forum alone do you see people using pointers with limited or no understanding of what pointers are? A pointer is an abstraction, albeit a low level one, that shields the programmer from details such as how the processor interacts with RAM.
    Right 98% of the time, and don't care about the other 3%.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems with Nested Structures and Arrays of Structures
    By Ignoramus in forum C Programming
    Replies: 4
    Last Post: 03-02-2010, 12:24 AM
  2. Accessing Structures Inside Structures
    By Mellowz in forum C Programming
    Replies: 1
    Last Post: 01-13-2008, 02:55 AM
  3. Structures, passing array of structures to function
    By saahmed in forum C Programming
    Replies: 10
    Last Post: 04-05-2006, 11:06 PM
  4. Accessing structures contained in structures
    By enigmaatj in forum C Programming
    Replies: 1
    Last Post: 04-18-2002, 08:53 AM
  5. Replies: 5
    Last Post: 04-11-2002, 11:29 AM

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