42.3k views
0 votes
Pointers are addresses and have a numerical value. You can print out the value of a pointer as cout << (unsigned)(p). Write a program to compare p, p + 1, q, and q + 1, where p is an int* and q is a double*. Explain the results.

1 Answer

2 votes

Answer:

#include <iostream>

using namespace std;

int main() {

int i = 2;

double j = 3;

int *p = &i;

double *q = &j;

cout << "p = " << p << ", p+1 = " << p+1 << endl;

cout << "q = " << q << ", q+1 = " << q+1 << endl;

return 0;

}

Step-by-step explanation:

In C++, pointers are variables or data types that hold the location of another variable in memory. It is denoted by asterisks in between the data type and pointer name during declaration.

The C++ source code above defines two pointers, "p" which is an integer pointer and "q", a double. The reference of the variables i and j are assigned to p and q respectively and the out of the pointers are the location of the i and j values in memory.

User Drmrgd
by
5.1k points