Hi, I am attempting to come to terms with pointers and have come up against a problem. By my understanding the code below should:

a, assign the value 3200 to total.
b, attach the memory address of total to ptr.
c, send the value at ptr to val.
d, display val (which will be the same as total)

but heres problem, if I then change the value of total as shown in the code the original value of 3200 comes back from val even if the value of total is different?

[

#include <iostream>
using namespace std;

int main()

{

int total;
it *ptr;
int val;
int c;

total = 3200;// assign 3200 to total
ptr = &total; // get address o total
val = *ptr; // get value at that address

cout << "Total is:" << val << '\n';

cout << "Enter total";

cin >> total;

cout << "Total is: <<val << '\n';

return 0;

}

]

Any help would be greatly appreciated.

Cheers

The variable called "val" is not a pointer, it is another (completely separate) integer. It will not automagically update. You must perform another assignment.
This code:

``````total = 3200;// assign 3200 to total
ptr = &total; // get address o total
val = *ptr; // get value at that address``````

is functionally equivalent to:

``````total = 3200;// assign 3200 to total
val = total; // get value at that address``````

However, if you change `cout << "Total is: <<val << '\n';` to `cout << "Total is: <<*ptr << '\n';` the output should update because you are using the pointer instead.

Hi,

val and total are on different memory locations, so if you assign a value to total it does not change the value of val.
With:

``val = *ptr;``

you have just copied the value from the memory location of total to the location of val. There is no way you can make a variable reference another location in memory then originally assigned by the compiler.

It is possible to make 2 variables reference the same memory location, but its use is only justified under certain VERY LIMITED circumstances. Really, the only proper use is passing information into and out of a function. Almost any other use really isn't considered good practice. This is called a "reference variable", you frequently see them described about the same time as pointers in texts. Generally, it's to point out a common syntax error when using pointers and show you that pointers aren't actually doing what you may think they are doing.

``````int anInt = 0;  //declare an integer
int& theSameInt = anInt;  //declare an integer reference
//anInt and theSameInt are now interchangable``````

Like I said, you really don't want to do this in practice. In a larger program, if you use 2 different names for the same variable, it makes the code hard to read causing confusion, and you don't want that.

I only mention this because it seems that you think this is what pointers do, but it's not. Pointers store addresses of values, they do not store actual values. The star/splat/asterisk that you place in front of the pointer's name is called the "dereference operator", it is used to tell C++ that you want the value stored at the address pointed by (stored at location number) the pointer.

Allow me an analogy:
You and I have a mutual friend, let's call him "John". I know where John lives but you don't. One day you ask me about John's house, what it looks like, does it have a nice garage, etc... Problem is, I don't know what his house looks like, I've never been there. All that I know is his mailing address. So, I give you his address and tell you to drive to his house. Once you get there, you see a nice 2-story with tan rough-sawn cedar siding and a maroon oval-lite front door. To the right of, and a little behind, it there is also a matching 4-stall garage filled with 2 cars, a truck, and several motorcycles.

In the analogy, you are the program and I'm a pointer to a (John's) house. I have an address and nothing more, it's up to you to go to the address I tell you to find out more.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.