Home Dynamic declaration of structure using pointer
Post
Cancel

Dynamic declaration of structure using pointer

We can dynamically declare sturrcture is C++ using just new and the name of the structure.To access its inner variables we have to use -> instead of . as it is a pointer ,however we can us . with * behind the pointer so that it becomes a structure variable.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include<iostream>
using namespace std;
struct boy
{
    char name[80];
    int age;
};
int main()
{
    boy *b1;
    b1= new boy;
    cout<<"Enter name:\n";
    cin>>b1->name;
    cout<<"Enter age:\n";
    cin>>b1->age;
    cout<<"Name is: "<<(*b1).name<<" Age is: "<<(*b1).age;
    return 0;
} 

OUTPUT:

output

This post is licensed under CC BY 4.0 by the author.

Trending Tags