Course Content
C++ Introduction
0/1
C++ Variables & Constants
0/1
C++Scope of Variable
0/1
C++ Keywords & Identifiers
0/1
C++ Data Types
0/1
C++ Basic I/O
0/1
C++ Type Conversion
0/1
C++ Operators
0/1
C++ Comments
0/1
C++ If-else
0/1
C++ Ternary Operator
0/1
C++ for Loop
0/1
C++ Ranged for Loop
0/1
C++ while/do-while Loop
0/1
C++ break Statement
0/1
C++ Continue Statement
0/1
C++ switch Statement
0/1
C++ goto Statement
0/1
C++ Functions
0/1
C++ User-defined Functions
0/1
C++ Default Arguments
0/1
C++ Storage Class
0/1
C++ Recursion
0/1
C++ Return by Reference
0/1
C++ Arrays
0/1
C++ Multi-dimentional Arrays
0/1
C++ Arrays & Function
0/1
C++ String
0/1
C++ Structure
0/1
C++ Structure & Functions
0/1
C++ Pointers to Structure
0/1
C++ Pointers
0/1
C++ Void Pointers
0/1
C++ Pointers & Arrays
0/1
C++ Pointers & Functions
0/1
C++ Dynamic Memory Allocation
0/1
C++ OOPs Concepts
0/1
C++ Objects and Class
0/1
C++ Constructors
0/1
C++ Destructors
0/1
C++ Constructor Overloading
0/1
C++ Objects & Function
0/1
C++ Enumeration
0/1
C++ Inheritance
0/1
C++ Inheritance Access Control
0/1
C++ Inheritance Types
0/1
C++ Polymorphism
0/1
C++ Function Overloading
0/1
C++ Function Overriding
0/1
C++ Operator Overloading
0/1
C++ Friend Function
0/1
C++ Virtual Function
0/1
C++ Abstract Class & Pure Virtual Function
0/1
C++ Encapsulation
0/1
C++ Abstraction
0/1
C++ Templates
0/1
C++ Exception Handling
0/1
C++ Multithreading
0/1
C++ Standard Library
0/1
C++ Programming Tutorials
About Lesson

C++ Pointers to Structures

In this tutorial, you’ll find relevant examples that will help you to work with pointers to access data within a structure with the help of examples.


Pointers to Structures

  • A pointer variable can be created not only for native types like (intfloatdouble etc.) but they can also be created for user defined types like structure.
  • Like we have pointers to int, char and other data-types, we also have pointers pointing to structures.
  • These pointers are called structure pointers.
  • We can also have pointer to a single structure variable, but it is mostly used when we are dealing with array of structure variables.

If you do not know what pointers are, visit C++ pointers.


Declaration and Use of Structure Pointers in C++

Just like other pointers, the structure pointers are declared by placing asterisk(*) in front of a structure pointer’s name.

Here is how you can create pointer for structures:

struct cat {
char name[10];
char breed[10];
int age;
char color[10];
};
struct cat spark;
// declaring a pointer to a structure of type struct cat
struct cat *ptr_cat;

This declares a pointer ptr_cat that can store the address of the variable of type struct dog.

We can now assign the address of variable spark to ptr_cat using & operator

ptr_cat = &spark;

Now ptr_cat points to the structure variable spark


Here is how you can create pointer for structures:

#include <iostream>
using namespace std;
                                                          
struct struct_name {
int i;
float f;
};
                                                          
int main() {
struct_name *ptr;
return 0;
}

This program creates a pointer ptr of type structure Struct_name.


Example 1: C++ Pointers to Structure

#include <iostream>
using namespace std;
struct Time{
    int min;
    float second;
};

int main(){
    Time *ptr, t;
    ptr = &t;

    cout << "Enter minutes: ";
    cin >> (*ptr).min;
    cout << "Enter seconds: ";
    cin >> (*ptr).second;
                                                                       
    cout << "Displaying information." << endl;
    cout << "Time = " << (*ptr).min << " minutes " << (*ptr).second << " seconds";
                                                                      
    return 0;
}

Output

Enter minutes: 2
Enter seconds: 20
Displaying information.
Time = 2 minutes 20 seconds

In this program, a pointer variable ptr and normal variable t of type structure Time is defined.

The address of variable t is stored to pointer variable, that is, ptr is pointing to variable t. Then, the member function of variable t is accessed using pointer.

Note: Since pointer ptr is pointing to variable t in this program, (*ptr).second and t.second is exact same cell. Similarly, (*ptr).min and t.min is exact same cell.

The syntax to access member function using pointer is ugly and there is alternative notation -> which is more common.

ptr->min is same as (*ptr).min
ptr->second is same as (*ptr).second

 

error: Content is protected !!