Accessing the Members of Nested Structure using Pointers




This video tutorial explains how to use pointers and arrow operator to access the nested structure in c++.
You will learn how to create a pointer to store the address of a nested structure, how to use pointers with nested structure pointers, how to access the values in the nested structures in detail with examples.

source code for this tutorial

#include <iostream>
#include<string>

using namespace std;

struct address{
    int house_no;
    string street_name;
};

struct student{
    string name;
    int rollno;
    address *addr;
};

int main()
{
student anil;
student *anilptr;

anilptr = &anil;

anilptr->name = "anil";
anilptr->rollno = 1245;

address adr = {65,"madamakki"};

anilptr->addr = &adr;

cout << anilptr->name << endl;
cout << anilptr->rollno << endl;

cout << anilptr->addr->house_no << endl;
cout << anilptr->addr->street_name << endl;

    return 0;
}