Thread: Further clarification on structs and pointers, please.

  1. #1
    Registered User
    Join Date
    Oct 2015
    Posts
    15

    Further clarification on structs and pointers, please.

    I've been lurking online for some more information on Structs and Pointers. I use them all the time for various things depending on what my needs are. I just want to make sure my thoughts line up with the way things should be and there are a few that I'm iffy on.

    I've read that if you just create a struct...

    Code:
    struct OBJECT{
        int x,y;
    }myobject;
    ...whenever you want to call upon that struct you have to always prefix it with struct myobject before using it. I've never done this. I just call the member

    Code:
    myobject.x
    And I've never had an issue doing it this way. Also I use typedef depending on my needs. However, according to what I have read, typedef just masks the namespace and the member id to read as the same thing.

    Code:
    typedef struct OBJECT{
       int x,y;
    }OBJECT;
    Again, according to the books it makes it so you don't have to write the word struct before calling your struct each time. Like I said, I've never had to write struct more than once for each declaration so I'm not so sure about that. Maybe it's old and that's why I don't see that use anymore? But in any case, I've also never had an issue with not using typdef on a struct and declaring it like this:

    Code:
    struct OBJECT{ // <---notice no typedef
       int x,y;
    }OBJECT;
    The above seems to work just fine as well.


    My second area is on the use of pointers, specifically with structs. In general I use * for a pointer and & when creating a pointer reference or accessing its address. But I've seen pointers without any operator being used. Example:

    Code:
    typedef struct OBJECT{
      int x,y;
    }OBJECT;
    
    OBJECT *chair; // using *
    
    // or
    
    OBJECT &chair; // using &
    
    // or
    
    OBJECT chair; // no operator
    While I have used all three ways, I am not so sure as of the last one and I can't find any good documentation on it other than the few times I've seen it used without much explanation.

    Thanks a bunch.

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Code:
    struct OBJECT{
        int x,y;
    }myobject;
    The above code creates an instance of OBJECT with the name of myobject. To create another instance of object you would need:
    Code:
    struct OBJECT myOtherObject;
    To access a member variable of the structure you need to state which variable you're interested ie myOtherObject, myobject, and access the variable using the dot (.) notation or if you created a pointer use pointer notation (->). Ie. myOtherObject.x myobject.y.

    Code:
    typedef struct OBJECT{
      int x,y;
    }MyNewObjectType;
    The typedef creates a new Type, in the case above named MyNewObjectType, it doesn't create an instance of that Type, just names a new type. You can now create a new instance of this type without using the struct keyword.

    Code:
    struct OBJECT test; // Still valid but you must use the struct keyword to make an instance of this structure.
    MyNewObjectType test1; // You don't need the struct keyword in this instance since MyNewObjectType is a Type of variable much like an int, or double.


    Jim

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 09-25-2014, 01:07 PM
  2. Newguy wants clarification on jumping into to C++ (pointers)
    By papagym177 in forum C++ Programming
    Replies: 5
    Last Post: 07-28-2014, 11:36 AM
  3. Pointers Clarification
    By pyroknife in forum C Programming
    Replies: 9
    Last Post: 01-13-2013, 03:33 AM
  4. Use Count smart pointers. Need clarification
    By Mario F. in forum C++ Programming
    Replies: 8
    Last Post: 06-26-2006, 03:07 PM
  5. passing structs & pointers to structs as arguments
    By Markallen85 in forum C Programming
    Replies: 6
    Last Post: 03-16-2004, 07:14 PM