Skip to main content

Featured

C++ Case Study.

 This case study is a program that can be used in a small library to include new books in the library, to check out books to people, and to return them. As this program is a practice in the use of linked lists, almost everything is implemented in terms of such lists. But to make the program more interesting, it uses linked lists of linked lists that also contain cross-references (see Figure 3.26). First, there could be a list including all authors of all books in the library. However, searching through such a list can be time-consuming, so the search can be sped up by choosing at least one of the two following strategies: ■ The list can be ordered alphabetically, and the search can be interrupted if we find the name, if we encounter an author’s name greater than the one we are searching for, or if we reach the end of list. ■ We can use an array of pointers to the author structures indexed with letters; each slot of the array points to the linked list of authors whose names start w...

C++ Pointers to Structures.

 

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.

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.

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 ptr and 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.

Notes:

  • Since pointer ptr is pointing to variable d in this program, (*ptr).inch and d.inch are equivalent. Similarly, (*ptr).feet and d.feet are equivalent.
  • However, if we are using pointers, it is far more preferable to access struct members using the -> operator, since the . operator has a higher precedence than the * operator.

    Hence, we enclose *ptr in brackets when using (*ptr).inch. Because of this, it is easier to make mistakes if both operators are used together in a single code.
     
    ptr->feet is same as (*ptr).feet
    ptr->inch is same as (*ptr).inc

Comments

Popular Posts