Like Tree1Likes
  • 1 Post By whiteflags

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

This is a discussion on using '.' over '->' operator in struct within the C++ Programming forums, part of the General Programming Boards category; I'm building trivial doubly circular linked list and I'm curious why I cannot do something such as: Code: cur = ...

  1. #1
    Registered User
    Join Date
    Jul 2012
    Posts
    87

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

    I'm building trivial doubly circular linked list and I'm curious why I cannot do something such as:
    Code:
    cur = cur.next
    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':
    Code:
    cur = (*cur).next//SAME AS syntactic sugar: cur =  cur->next
    Code:
    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)
    Code:
    #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 
        node_1.data = 8;
        node_1.next = &node_1; node_1.back = &node_1;
        head = &node_1;
        
        doubly_cll node_2;
        node_2.data = 99;
        node_2.next = &node_1; node_2.back = &node_1;
        node_1.next = &node_2;//tail pts to new front node
        node_1.back = &node_2;
        head = &node_2;
        
        doubly_cll node_3;//new front node
        node_3.data = 1;
        node_3.next = &node_2; node_3.back = &node_1;//update new node's ptrs
        node_2.back = &node_3;//update old head's ptrs
        node_1.next = &node_3;//update tail's ptrs
        head = &node_3;
            
        //OUTPUT expected:  1  99  8
        doubly_cll* cur = head;
        do
        {
            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
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,625
    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.
    rogster001 likes this.

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

    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.

    Soma

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, 01:33 PM
  3. Replies: 3
    Last Post: 12-09-2008, 10:19 AM
  4. Replies: 2
    Last Post: 07-07-2008, 03:46 AM
  5. Replies: 1
    Last Post: 07-07-2008, 03:38 AM

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