I'm working on a program that uses pointers to set the values of variables. For example, to set price to 19.95, I won't use the variable price, but the pointer variable *p_price.
The code below produces the following:
address of price=0x22fec8
contents of price=19.95
address of *p_price=0x22ffe0
contents of p_price=0x22ffe0
contents of *p_price=19.95
I'm trying to get the middle one to display the address of p_price, not *p_price. However, changing the code to display &p_price causes the program to crash without any indication of what's wrong.
Should the address of price be &*p_price
and the address of p_price be &price?
#include <iostream>
#include <iomanip>
#include <random> // needed for Orwell devcpp
using namespace std;
int main(){
float * p_price;
*p_price=19.95;
float price = *p_price;
cout <<"address of price="<<&price<<endl;
cout <<"contents of price="<<price<<endl;
cout <<"address of *p_price="<<&*p_price<<endl;
cout <<"contents of p_price="<<p_price<<endl;
cout <<"contents of *p_price="<<* p_price<<endl;
}