Skip to content
Related Articles

Related Articles

Improve Article

Structure Pointer

  • Last Updated : 17 Aug, 2021

Structure Pointer: It is defined as the pointer which points to the address of the memory block that stores a structure is known as the structure pointer. Below is an example of the same:
Example:

struct point
{
   int value;
};

// Driver Code
int main()
{
    struct point s;
    struct point *ptr = &s;
    return 0;
}

In the above code s is an instance of struct point and ptr is the struct pointer because it is storing the address of struct point. 

Below is the program to illustrate the above concepts:

C




// C program to illustrate the
// structure pointer
 
#include <stdio.h>
 
// Structure declaration for
// vertices
struct point {
    int x;
    int y;
};
 
// Structure declaration for
// rectangle
struct rect {
 
    // An object left is declared
    // with 'point'
    struct point left;
 
    // An object right is declared
    // with 'point'
    struct point right;
};
 
// Function to calculate area of
// the given rectangle
void areaOfRectangle(struct rect r)
{
    // Find the area of the rectangle
    // using variables of point
    // structure where variables of
    // point structure is accessed
    // by left and right objects
    int area
        = (r.right.x - r.left.x)
          * (r.right.y - r.left.y);
 
    // Print the area
    printf("%d", area);
}
 
// Driver Code
int main()
{
    // Initialize variable 'r'
    // with vertices of rectangle
    struct rect r = { { 0, 0 }, { 1, 1 } };
 
    // Function Call
    areaOfRectangle(r);
 
    return 0;
}

C++




// C++ program to illustrate the
// structure pointer
#include <iostream>
#include <stdio.h>
using namespace std;
 
// Structure declaration for
// vertices
struct point {
    int x;
    int y;
};
 
// Structure declaration for
// rectangle
struct rect {
 
    // An object left is declared
    // with 'point'
    struct point left;
 
    // An object right is declared
    // with 'point'
    struct point right;
};
 
// Function to calculate area of
// the given rectangle
void areaOfRectangle(struct rect r)
{
    // Find the area of the rectangle
    // using variables of point
    // structure where variables of
    // point structure is accessed
    // by left and right objects
    int area
        = (r.right.x - r.left.x)
          * (r.right.y - r.left.y);
 
    // Print the area
    cout << area;
}
 
// Driver Code
int main()
{
    // Initialize variable 'r'
    // with vertices of rectangle
    struct rect r = { { 0, 0 }, { 1, 1 } };
 
    // Function Call
    areaOfRectangle(r);
 
    return 0;
}
Output: 
1

 

Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.



My Personal Notes arrow_drop_up
Recommended Articles
Page :