Structure Pointer
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