Thread: Structures -Help

  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
    6,815
    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%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    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

  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
    28,413
    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.
    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

  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
    6,815
    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%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  9. #9
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    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 ?)

  10. #10
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    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%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

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, 01:24 AM
  2. Accessing Structures Inside Structures
    By Mellowz in forum C Programming
    Replies: 1
    Last Post: 01-13-2008, 03: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