Loading...
C++ Pointers to Structures

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 (int, float, double 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

Next Tutorial

We hope that this tutorial helped you develop better understanding of the concept of Pointers to Structure in C++.

Keep Learning : )

In the next tutorial, you'll learn about C++ Pointers.

- Related Topics