Thread: using '.' over '->' operator in struct

  1. #1
    Registered User
    Join Date
    Jul 2012

    using '.' over '->' operator in struct

    I'm building trivial doubly circular linked list and I'm curious why I cannot do something such as:
    cur =
    since 'cur' is a pointer and the member 'next' is also a pointer so that's why I'm unclear why 'cur' must be dereferenced to access the pointer member: 'next':
    cur = (*cur).next//SAME AS syntactic sugar: cur =  cur->next
    struct doubly_cll//doubly circular linked list obj
        int data;
        doubly_cll* next;
        doubly_cll* back;
    In main, I'm manually inserting new node at front and 'tail' node's next pointer points to newnode (and head pointer points to new node of course)
    #include <iostream>
    using namespace std;
    int main()
        doubly_cll* head = NULL;
        // doubly_cll* tail = NULL;//NB: don't really need a tail ptr b/c  I'm only inserting new node @ front, so immediately make cur(old) head's  next node's next
        //    to pt to newNode BEFORE making head pt to new node (that way  we don't lose track of the "tail" node since no NULL indicator)
        doubly_cll node_1;//this is the 'tail' node = 8; = &node_1; node_1.back = &node_1;
        head = &node_1;
        doubly_cll node_2; = 99; = &node_1; node_2.back = &node_1; = &node_2;//tail pts to new front node
        node_1.back = &node_2;
        head = &node_2;
        doubly_cll node_3;//new front node = 1; = &node_2; node_3.back = &node_1;//update new node's ptrs
        node_2.back = &node_3;//update old head's ptrs = &node_3;//update tail's ptrs
        head = &node_3;
        //OUTPUT expected:  1  99  8
        doubly_cll* cur = head;
            cout << "cur node: " << (*cur).data << ", ";//<=> cur->data ('->' just syntactic sugar)
            cur = (*cur).next;//cur is a ptr, member 'next' is a ptr so why doesn't this work? a ptr cannot hold a member?
        while( cur != head );//stop when we pt back to head node (no NULL so this is stop condition)
        return 0;

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    United States
    It might not seem very intuitive, but you want to access a member of the pointee, so it doesn't matter what type of member it is, you still have to dereference. The pointer value does not have any members to be accessed with the dot operator.

  3. #3
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    why I cannot do something such as

    Once again, you've basically answered your own question but missed it because you are too close to the situation.

    You can't do that because, as you say, `cur' is a pointer; the variable `cur' is not a structure; it can't then have members as a structure has members.

    The variable `cur' is only a pointer to a structure. It is obvious, as from the earlier thread, that a pointer must be "dereferenced" to actually access the thing where the pointer points.


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. using comparative operator on a typedef struct member
    By csharp100 in forum C Programming
    Replies: 2
    Last Post: 05-02-2012, 07:52 AM
  2. Overloaded a struct operator==
    By Programmer_P in forum C++ Programming
    Replies: 11
    Last Post: 03-04-2011, 02:33 PM
  3. Replies: 3
    Last Post: 12-09-2008, 11:19 AM
  4. Replies: 2
    Last Post: 07-07-2008, 03:46 AM
  5. Replies: 1
    Last Post: 07-07-2008, 03:38 AM