Thread: Structure.Element or Structure->Element?

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    5

    Structure.Element or Structure->Element?

    What is the difference between the two methods of accessing an element within a structure and when is each appropriate?

    Code:
    typedef struct{
       int a;
       int b;
    } myStruct;
    
    int main(void)
    {
    myStruct person;
    
    person.a = 1;
    person->b = 2;
    }
    This is not a working program. I was just trying to give an example to further explain my question.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    If Structure is an object of a struct type that has a member named Element, then Structure.Element is correct.

    If Structure is a pointer to an object of a struct type that has a member named Element, then Structure->Element is correct: it is equivalent to (*Structure).Element.
    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
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    You use the .member when you have an actual instance of your structure.
    You use the ->member when you have a pointer to an instance.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Use "->" when the structure/union you are using is a pointer
    Use "." when structure/union is not a pointer.

    Edit - Include union and was too slow for Salem
    Last edited by Click_here; 10-23-2012 at 12:46 AM.
    Fact - Beethoven wrote his first symphony in C

  5. #5
    Registered User
    Join Date
    Oct 2012
    Posts
    5
    Does this carry through if there is a structure within a structure?


    Code:
    typedef struct{
       int a;
    } myStruct2;
    
    typedef struct{
       myStruct2 struct2;
    } myStruct1;
    
    int main(void)
    {
       myStruct1 struct1;
       myStruct1 *pstruct;
    
       pstruct = &struct1;
    
       struct1.struct2.a = 1;
       pstruct->struct2->a = 2;
    }

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Click_here
    was too slow for Salem
    Who was too slow for me

    Quote Originally Posted by circuits2
    Does this carry through if there is a structure within a structure?
    Yes.
    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
    Oct 2012
    Posts
    5
    Thank you all for the quick replies!

  8. #8
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Quote Originally Posted by laserlight
    Who was too slow for me
    I don't even try to be faster than you, laserlight!
    Fact - Beethoven wrote his first symphony in C

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by circuits2
    Thank you all for the quick replies!
    You're welcome

    By the way, I know you stated that your code example is just for example and is not expected to work, but perhaps you should try to compile your example in post #5 as you might learn something.

    Quote Originally Posted by Click_here
    I don't even try to be faster than you, laserlight!
    Haha!
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Structure element not printing
    By SasDutta in forum C Programming
    Replies: 4
    Last Post: 05-16-2011, 03:58 PM
  2. Pointer to structure element
    By looza in forum C Programming
    Replies: 8
    Last Post: 03-24-2011, 03:37 PM
  3. referencing a structure element from another structure
    By ajitht1986 in forum C Programming
    Replies: 5
    Last Post: 02-08-2011, 03:36 AM
  4. data structure element
    By aayu09 in forum C Programming
    Replies: 1
    Last Post: 03-31-2010, 08:53 PM
  5. Pointer to a structure element
    By Bladactania in forum C Programming
    Replies: 7
    Last Post: 04-23-2009, 04:57 PM