C and C++ programming language

This page contains a collection examples on basic concepts of C programming like: loops, functions, pointers, structures etc.

Menu

  • Home
  • c programing
  • C++ programming
  • About us
  • Disclaimer
  • Privacy Policy
  • Terms and conditions
  • Contact us

Tuesday, October 23, 2018

C++ Pointers to Structure

C++ Pointers to Structure

In this article, you'll find relevant examples that will help you to work with pointers to access data within a structure.



A pointer variable can be created not only for native types like (int, float, doubleetc.) but they can also be created for user defined types like structure.
If you do not know what pointers are, visit C++ pointers.
Here is how you can create pointer for structures:
#include <iostream>
using namespace std;

struct temp {
    int i;
    float f;
};

int main() {
    temp *ptr;
    return 0;
}
This program creates a pointer ptr of type structure temp.

Example: Pointers to Structure

#include <iostream>
using namespace std;

struct Distance
{
    int feet;
    float inch;
};

int main()
{
    Distance *ptr, d;

    ptr = &d;
    
    cout << "Enter feet: ";
    cin >> (*ptr).feet;
    cout << "Enter inch: ";
    cin >> (*ptr).inch;
 
    cout << "Displaying information." << endl;
    cout << "Distance = " << (*ptr).feet << " feet " << (*ptr).inch << " inches";

    return 0;
}
Output

Enter feet: 4
Enter inch: 3.5
Displaying information.
Distance = 4 feet 3.5 inches
In this program, a pointer variable ptrand normal variable d of type structure Distance is defined.
The address of variable d is stored to pointer variable, that is, ptr is pointing to variable d. Then, the member function of variable d is accessed using pointer.
Note: Since pointer ptr is pointing to variable d in this program, (*ptr).inch and d.inch is exact same cell. Similarly, (*ptr).feet and d.feet is exact same cell.
The syntax to access member function using pointer is ugly and there is alternative notation -> which is more common.
ptr->feet is same as (*ptr).feet
ptr->inch is same as (*ptr).inch

- October 23, 2018
Email ThisBlogThis!Share to XShare to FacebookShare to Pinterest
Labels: CPP programming

No comments:

Post a Comment

Newer Post Older Post Home
Subscribe to: Post Comments (Atom)

Popular Posts

  • C++ Classes and Objects
    C++ Classes and Objects In this article, you will learn to work with objects and classes in C++ programming. C++ is a multi-paradigm ...
  • C++ Default Argument
    C++ Programming Default Arguments (Parameters) In this article, you'll learn what are default arguments, how are they used and neces...
  • C++ OBJECTS & FUNCTION
    How to pass and return object from a function in C++? In this article, you will learn to pass objects to a function and return object fr...
Powered By Blogger

Search This Blog

Blog Archive

  • October 2018 (39)

Labels

  • c programming (10)
  • CPP ARAYS AND STRING (4)
  • CPP FLOW CONTROL or LOOP (6)
  • CPP FUNCTION (5)
  • CPP OBJECT AND CLASS (4)
  • CPP POINTERS (4)
  • CPP programming (29)
  • CPP STRACTURES (1)

About Me

RANJIT KUMAR SAHU
View my complete profile

Report Abuse

NAVIGATION

  • About us
  • Disclaimer
  • Privacy Policy
  • Contact us
  • Terms and conditions
Simple theme. Powered by Blogger.