C++ Pointers and Structs: Code Examples and Explanations
C++ Pointers and Structs: Code Examples
Pointer Basics
int x;
int y;
int *p = &x;
int *q = &y;
x = 35; y = 46;
p = q;
*p = 78;
cout << x << endl;
cout << y << endl;
int *p;
int *q;
p = new int;
q = p;
*p = 46;
*q = 39;
cout << *p << endl;
int *p;
int *q;
p = new int;
*p = 43;
q = p;
*q = 52;
p = new int;
*p = 78;
q = new int;
*q = *p;
cout << *p << endl;
Pointer Example 1: Storing Variable Addresses
// This program stores the address of a variable in a pointer.
#include <iostream>
using namespace std;
int main()
{
double x = 22.6;
double *p;
double y;
p = &x;
y = *p + 20;
cout << *p << endl;
cout << y << endl;
cout << x << endl;
cin.ignore(100, '\n');
cin.get();
return 0;
}
Pointer Example 2: Indirection Operator
// This program demonstrates the use of the indirection operator.
#include <iostream>
using namespace std;
int main()
{
int x = 25; // int variable
int *ptr; // Pointer variable, can point to an int
ptr = &x; // Store the address of x in ptr
// Use both x and ptr to display the value in x.
cout << "Here is the value in x, printed twice:\n";
cout << x << " " << *ptr << endl;
// Assign 100 to the location pointed to by ptr.
// This will actually assign 100 to x.
*ptr = 100;
// Use both x and ptr to display the value in x.
cout << "Once again, here is the value in x:\n";
cout << x << " " << *ptr << endl;
return 0;
}
Pointer Example 3: Pointing to Different Variables
// This program demonstrates the ability of a pointer to point to different variables.
#include <iostream>
using namespace std;
int main()
{
int x = 25, y = 50, z = 75; // Three int variables
int *ptr; // Pointer variable
// Display the contents of x, y, and z.
cout << "Here are the values of x, y, and z:\n";
cout << x << " " << y << " " << z << endl;
// Use the pointer to manipulate x, y, and z.
ptr = &x; // Store the address of x in ptr
*ptr *= 2; // Multiply value in x by 2
ptr = &y; // Store the address of y in ptr
*ptr *= 2; // Multiply value in y by 2
ptr = &z; // Store the address of z in ptr
*ptr *= 2; // Multiply value in z by 2
// Display the contents of x, y, and z.
cout << "Once again, here are the values of x, y, and z:\n";
cout << x << " " << y << " " << z << endl;
return 0;
}
Structs
Structs
#include <iostream>
using namespace std;
struct rectangleData
{
double length;
double width;
};
double rectArea(double x, double y);
double rectperimeter (double, double);
int main()
{
rectangleData rect1;
cin >> rect1.length ;
cin >> rect1.width ;
cout << "Rectangle area is " << rectArea(rect1.length, rect1.width) << endl;
cin.ignore(100, '\n');
cin.get();
return 0;
}
double rectArea(double x, double y)
{
return x*y;
}
Struct Example: Time
// structs/time-1.cpp – Shows simple use of a struct. // Fred Swartz – 2003-09-09
#include <iostream>
using namespace std;
// Define a new type
struct Time
{
int hours;
int minutes;
int seconds;
};
// Prototypes
int toSeconds(Time now);
int main()
{
Time t;
cout << "Please enter: hours, mins, seconds: ";
while (cin >> t.hours >> t.minutes >> t.seconds)
{
cout << "Total seconds: " << toSeconds(t) << endl;
}
cin.ignore(100, '\n');
cin.get();
return 0;
}
// Convert Time struct to seconds
int toSeconds(Time now)
{
return 3600*now.hours + 60*now.minutes + now.seconds;
}