Note: We don't really need to use the temp variable for most compilers and C++ versions. Instead, we can simply use the following code:
p = getData(p);
p = getData(p);
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, double 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:
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, double 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 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;
}
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
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.
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
🔘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
C++ Enumeration
In this article, you will learn to work with enumeration (enum). Also, you will learn where enums are commonly used in C++ programming.
An enumeration is a user-defined data type that consists of integral constants. To define an enumeration, keyword enum is used.
In this article, you will learn to work with enumeration (enum). Also, you will learn where enums are commonly used in C++ programming.
An enumeration is a user-defined data type that consists of integral constants. To define an enumeration, keyword enum is used.
Here, the name of the enumeration is season.
And, spring, summer and winter are values of type season.
By default, spring is 0, summer is 1 and so on. You can change the default value of an enum element during declaration (if necessary).
And, spring, summer and winter are values of type season.
By default, spring is 0, summer is 1 and so on. You can change the default value of an enum element during declaration (if necessary).
Enumerated Type Declaration
When you create an enumerated type, only blueprint for the variable is created. Here's how you can create variables of enum type.
enum boolean { false, true };
// inside function
enum boolean check;
Here, a variable check of type enum boolean is created.
When you create an enumerated type, only blueprint for the variable is created. Here's how you can create variables of enum type.
enum boolean { false, true };
// inside function
enum boolean check;
Here, a variable check of type enum boolean is created.
Here is another way to declare same check variable using different syntax.
enum boolean
{
false, true
} check;
enum boolean
{
false, true
} check;