Skip to content
Related Articles

Related Articles

Structure Pointer
  • Last Updated : 19 Jan, 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

filter_none

edit
close

play_arrow

link
brightness_4
code

// C program to illustrate the
// structure pointer
  
#include <stdio.h>
  
// Structure declaration for
// vertices
struct point {
    int x;
    int y;
};
  
// Strcuture 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;
}

chevron_right


C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
};
  
// Strcuture 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;
}

chevron_right


Output:

1

Attention reader! Don’t stop learning now. Get hold of all the important C++ Foundation and STL concepts with the C++ Foundation and STL courses at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up
Recommended Articles
Page :