C++ – Pointer to Structure
Pointer to structure in C++ can also be referred to as Structure Pointer. A structure Pointer in C++ is defined as the pointer which points to the address of the memory block that stores a structure. Below is an example of the same:
Syntax:
struct name_of_structure *ptr;
// Initialization of structure is done as shown below
ptr = &structure_variable;
Example 1:
C++
#include <iostream>
using namespace std;
struct point {
int value;
};
int main()
{
struct point g;
struct point* ptr = &g;
return 0;
}
|
In the above code g is an instance of struct point and ptr is the struct pointer because it is storing the address of struct point.
Example 2:
C++
#include <iostream>
#include <stdio.h>
using namespace std;
struct GFG {
int x;
int y;
};
struct square {
struct GFG left;
struct GFG right;
};
void area_Square( struct square s)
{
int area = (s.right.x) * (s.left.x);
cout << area << endl;
}
int main()
{
struct square s = { { 4, 4 }, { 4, 4 } };
area_Square(s);
return 0;
}
|
Last Updated :
07 Oct, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...