1
#include<iostream>
    using namespace std;
    int main()
    {
    struct node
    {
        int data;
        struct node *next;
    };

    struct node *node1;

    struct node node2;

    node2.data = 200;
    node2.next = NULL;

    cout<<"address of node2: "<<&node2<<endl;

    cout<<"address of node2.data: "<<&node2.data<<endl;

    cout<<"address of node2.next: "<<&node2.next<<endl;

    cout<<"value of node2 data: "<<node2.data<<endl;

    cout<<"value of node2 next is: "<<node2.next<<endl;

    node1 = (struct node*)malloc(sizeof(node));

    node1->data = 100;

    node1->next = NULL;

    cout<<"value of node1 data: "<<node1->data<<endl;

    cout<<"value of node1 next: "<<node1->next<<endl;

    cout<<"address of node1 variable is: "<<&node1<<endl;

    cout<<"address of node1 data variable is: "<<&node1->data<<endl;

    cout<<"address of node1 next variable is: "<<&node1->next<<endl;

    cout<<"value stored at node1 variable is: "<<node1<<endl;

    }

I wanted to print the address of the members of a struct variable using the pointers to that structure. As it can be seen in my above code example, I have used &node1->next and &node1->data to print the addresses. It seems to be printing the correct addresses because I am able to access the values by dereferencing the address returned by &node1->next and &node1->data. *(&node1->next) and *(&node1->data) returns the values correctly.

But I do not understand how the notations "&node1-> data" and "&node1->next" are returning the address of the members of the struct variable. I accidentally discovered that &node1->data and &node1->next printed the addresses. Whereas with other notations like &node2.data and &node2.next, I was able to logically come up with the notations to print the addresses, but while using pointer to struct to print the address, I accidentally discovered them instead of being able to logically come up with the correct notation.

I want to know whether whatever I have come up with is the correct usage to print the addresses of the member variables and if yes how is it the correct representation ?

7
  • @tobi303 question is how would you print the address of the members of a struct variable using pointer to that struct ? would you use &pointer_var->data_member ? and if yes, how does this actually return data ? it is confusing to visualize this notation in the context of accessing addresses using it Commented May 12, 2017 at 15:17
  • Why are you using malloc in C++? Commented May 12, 2017 at 15:28
  • I think I figured the answer, but I would still like to hear from experienced developers in C/C++. we use notation (*node1).next or node1->next to access(or print) value of next variable in struct. And in order to print the address we would just append the & sign to &(node1->next) or &((*node1).next). The use of brackets makes it more easier to understand. Commented May 12, 2017 at 15:29
  • @tobi303 thanks a lot ! that is a correct explanation. The mistake I did was I was trying to understand the pointers to struct by comparing it with a normal pointer to an integer variable. If we had a pointer to integer variable, then to print that integer variables address, we would simply print the value in the pointer (cout<<pointer_var;). Pointers to struct work differently. The confusion arises because of the dereferencing operator " * ". Like in example &((*node1).data), we already have a star, so ideally a beginner like me would expect node1.data to return the address. haha Commented May 12, 2017 at 15:35
  • actually, no it wasnt a good explanation. I will delete the comments and refer you to the answer that was given in the meantime Commented May 12, 2017 at 15:37

1 Answer 1

1

I want to know whether whatever I have come up with is the correct usage to print the addresses of the member variables and if yes how is it the correct representation ?

Yes, it is correct.

The indirection operator or "arrow" -> returns a reference to a member of the pointed object. So, the argument of the address-of-operator & is the member. The adderess-of returns the address of that object i.e. the address of the member. Therefore, logically the correct way to get the address of the member is to first apply arrow operator to get the object, and then the address-of-operator like so:

auto* pointer_to_object = get_the_object();
auto* address_of_member = &pointer_to_object->name_of_member;

It seems to be printing the correct addresses because I am able to access the values by dereferencing the address returned by &node1->next and &node1->data. *(&node1->next) and *(&node1->data) returns the values correctly.

This should be quite obvious. The dereference operation is the inverse of address-of operation, so applying those two in sequence will cancel each other out. *&node1->data is equivalent to node1->data, which indeed returns you the value of the member.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the answer. 1. "arrow" -> returns a reference to a member of the pointed object", so in my example the member would be the "data" variable and object would be "node1" (pointer to structure) ? 2. If arrow returned just reference to the data member, then how are we able to print the value stored in data by using node1->data ? (please bear with me if this is a bad question)
@livetolearn 1. The object that I talk about would be *node1 i.e. the object that node1 points to. 2. Because that's how references work.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.