The Wayback Machine - https://web.archive.org/web/20240118171320/https://www.geeksforgeeks.org/cpp-pointer-to-structure/
Open In App
Related Articles

C++ – Pointer to Structure

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

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++




// C++ program to demonstrate Pointer to Structure
#include <iostream>
 
using namespace std;
 
struct point {
    int value;
};
 
int main()
{
 
    struct point g;
    // Initialization of the structure pointer
    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++




// C++ program to Demonstrate Pointer to Structure
#include <iostream>
#include <stdio.h>
 
using namespace std;
 
// Structure declaration for
// vertices
struct GFG {
    int x;
    int y;
};
 
// Structure declaration for
// Square
struct square {
 
    // An object left is declared
    // with 'GFG'
    struct GFG left;
 
    // An object right is declared
    // with 'GFG'
    struct GFG right;
};
 
// Function to calculate area of
// the given Square
void area_Square(struct square s)
{
    // Find the area of the Square
    // using variables of point
    // structure where variables of
    // point structure is accessed
    // by left and right objects
    int area = (s.right.x) * (s.left.x);
 
    // Print the area
    cout << area << endl;
}
 
// Driver Code
int main()
{
    // Initialize variable 's'
    // with vertices of Square
    struct square s = { { 4, 4 }, { 4, 4 } };
 
    // Function Call
    area_Square(s);
 
    return 0;
}


Output

16

Learn to code easily with our course Coding for Everyone. This course is accessible and designed for everyone, even if you're new to coding. Start today and join millions on a journey to improve your skills!

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated : 07 Oct, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads