1

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;
}

4 Answers 4

1

The problem is that you assign a value to a pointer for which you didn't allocate memory. You are dereferencing *p_price, then assign 19.95 to it, but what address are you dereferencing? Because no memory was allocated for it, it points to some random location in memory, and this causes UB (undefined behaviour)

float * p_price; // no memory is allocated!!!!
// need to allocate memory for p_price, BEFORE dereferencing 
*p_price=19.95; // this is Undefined Behaviour
Sign up to request clarification or add additional context in comments.

3 Comments

To keep the contents of p_price as the address, i should declare a new variable and set p_price equal to the address of that then?
Yes. A pointer only stores an adress, but as vsoftco said something needs to store the float number too...
yes, or do float *p_price = new float;, and now you have some reserved memory location to which p_price points. THEN, you can dereference it and safely put 19.95 into that memory slot.
1
int main(){
     float * p_price;
    *p_price=19.95;

should be

int main(){
    float price;
     float * p_price = &price;
    *p_price=19.95;

a pointer has to point at something if you want to use it.

Comments

0

In your code p_price is a pointer to float, but you dereference it before assigning it to point to a float instance. Try this instead:

int main(){
    float price;
    float * p_price = &price;
    *p_price=19.95;

    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;
    return 0;
}

Comments

0

Hmm, as I can see you are creating pointer to float, but this pointer is pointing to nothing.

int main() {
    //Bad code
    float* pointer;    //Okay, we have pointer, but assigned to nothing
    *pointer = 19.95f; //We are changing value of variable under pointer to 19.95...
                       //WAIT what? What variable!?
    //////////////////////////////////////////////////////////////////////
    //Good code
    float* pointer2 = new float(0f);
    //We are creating pointer and also variable with value of 0
    //Pointer is assigned to variable with value of 0, and that is good
    *pointer2 = 19.95f;
    //Now we are changing value of variable with 0 to 19.95, and that is okay.
    delete pointer2;
    //But in the end, we need to delete our allocated value, because we don't need this variable anymore, and also, read something about memory leek
    return 0;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.