techpreparation-homepage

Home  Interview Questions  Aptitude Questions  Tutorials  Placement Papers  Search  Resume Guide  Soft Skills  Video  Forum  Blog

  C++ Programming Tutorials
Basics of C++

Structure of a program
Variables Data types
Constants
Operators
Basic Input/output

Control Structures
Control Structures
Functions (I)
Functions (II)

Compound Data Types
Arrays
Character Sequences
Pointers
Dynamic Memory
Data Structures
Other Data Types

Object Oriented Programming
Classes [I]
Classes [II]
Friendship & Inheritance
Polymorphism

Advanced Concepts
Templates
Namespaces
Exceptions
Type Casting
Preprocessor Directives

C++ Standard Library
Input/output with Files

Soft Skills
Communication Skills
Leadership Skills
                              .........More

Subscribe to our Newsletters
Name:
Email:

 

 

  

C++ Programming Tutorials

Pointers to structures
Like any other type, structures can be pointed by its own type of pointers:

struct movies_t {
string title;
int year;
};

movies_t amovie;
movies_t * pmovie;

Here amovie is an object of structure type movies_t, and pmovie is a pointer to point to objects of structure type movies_t. So, the following code would also be valid:

pmovie = &amovie;

The value of the pointer pmovie would be assigned to a reference to the object amovie (its memory address).

We will now go with another example that includes pointers, which will serve to introduce a new operator: the arrow operator (->):

// pointers to structures
#include <iostream>
#include <string>
#include <sstream>
using namespace std;

struct movies_t {
string title;
int year;
};

int main ()
{
string mystr;

movies_t amovie;
movies_t * pmovie;
pmovie = &amovie;

cout << "Enter title: ";
getline (cin, pmovie->title);
cout << "Enter year: ";
getline (cin, mystr);
(stringstream) mystr >> pmovie->year;

cout << "\nYou have entered:\n";
cout << pmovie->title;
cout << " (" << pmovie->year << ")\n";

return 0;
}
Enter title: Invasion of the body snatchers
Enter year: 1978

You have entered:
Invasion of the body snatchers (1978)
























The previous code includes an important introduction: the arrow operator (->). This is a dereference operator that is used exclusively with pointers to objects with members. This operator serves to access a member of an object to which we have a reference. In the example we used:

pmovie->title

Which is for all purposes equivalent to:

(*pmovie).title

Both expressions pmovie->title and (*pmovie).title are valid and both mean that we are evaluating the member title of the data structure pointed by a pointer called pmovie. It must be clearly differentiated from:

*pmovie.title

which is equivalent to:

*(pmovie.title)

And that would access the value pointed by a hypothetical pointer member called title of the structure object pmovie (which in this case would not be a pointer). The following panel summarizes possible combinations of pointers and structure members:

Expression What is evaluated Equivalent
a.b Member b of object a  
a->b Member b of object pointed by a (*a).b
*a.b Value pointed by member b of object a *(a.b)

Nesting structures
Structures can also be nested so that a valid element of a structure can also be on its turn another structure.

struct movies_t {
string title;
int year;
};

struct friends_t {
string name;
string email;
movies_t favorite_movie;
} charlie, maria;

friends_t * pfriends = &charlie;

After the previous declaration we could use any of the following expressions:

charlie.name
maria.favorite_movie.title
charlie.favorite_movie.year
pfriends->favorite_movie.year

(where, by the way, the last two expressions refer to the same member).

NEXT >> Other Data Types

Have a Question ? post your questions here. It will be answered as soon as possible.

Check C Aptitude Questions for more C Aptitude Interview Questions with Answers

Check C Interview Questions for more C Interview Questions with Answers.