简体   繁体   中英

Am I understanding pointers correctly? C++

I am learning about pointers in C++ and I have read an article on it and I think I understand it, though I just wanted some clarification on the pseudo code I wrote.

int someGameHealthAddress = 1693;
int healthIWantItToBe = 20;
int * newHealthValue;

newHealthValue = someGameHealthAddress;
*newHealthValue = healthIWantItToBe;

So is the above right? Like the way it works?

EDIT: Thank you all for answering, glad I got this down now. You've been a large help :) EDIT2: I'm quite proud I've got the hang of this now. By the looks of it a lot of people have trouble understanding pointers.

If someGameHealthAddress is supposed to be an address then you need to declare it as such. Eg:

int someGameHealth = 1693;
int healthIWantItToBe = 20;
int * someGameHealthAddress; //this is the pointer to an int, which is basically its address

someGameHealthAddress = &someGameHealth;    // take address of someGameHealth
*someGameHealthAddress = healthIWantItToBe; // modify the value it points to

In your code this line was wrong:

newHealthValue = someGameHealthAddress;

Because it missmatches the types of variables, it's like int* = int .

Note, that this is possible to cast from and integer type to pointer type, it's almost always a bug, because you nearly never know the absolute value of variable's address in memory. You usually locate something, and then work with relative offsets. This is usually the case when you are doing some in-memory hacking, which your example seems to be.

Almost. In order to get a pointer to a variable, you need the "address of" operator & :

// Set newHealthValue to point to someGameHealth
newHealthValue = &someGameHealth;

(I removed "Address" from the variable name, since it isn't an address. The pointer now contains its address).

Then your final line of code will change the value of the object that newHealthValue points to, ie it will change someGameHealth .

This statement is wrong:

newHealthValue = someGameHealthAddress;

Because the left side has type pointer and right side is an integer. You have to make sure types match in assignments. To get the address of the someGameHealthAddress , use & :

newHealthValue = &someGameHealthAddress;

Now the types match because the right side is an address of integer, and thus a pointer.

Depending if you want the value of the pointer to be 1693, or you want the pointer to point to the address of the variable someGameHealthAddress

1.assigns newHealthValue value to someGameHealthAddress value

*newHealthValue = someGameHealthAddress; 
  1. assigns the newHealthValue to point to the address of the someGameHealthAddress variable

    *newHealthValue = &someGameHealthAddress;

  2. assigns the address of newHealthValue to the value of the someGameHealthAddress variable

    &newHealthValue = someGameHealthAddress;

  3. assigns the address of the newHealthValue to the address of the someGameHealthAddress variable

    &newHealthValue = &someGameHealthAddress;

* and & are two operators used in c++

& means "the address off"

ie, &p means address of p .

* means "value in the location"

*p means value in the address stored in p.

For that p must me a pointer.(because p should keep an address).

Here newHealthValue = someGameHealthAddress; will give compilation error.because someGameHealthAddress is an integer and newHealthValue is integer pointer. int*=int is type miss-match

You can store the address of someGameHealthAddress using the following statement

newHealthValue = &someGameHealthAddress ;

which means newHealthValue = (address of)someGameHealthAddress

*newHealthValue = healthIWantItToBe; is syntactically correct because it stores the value of healthIWantItToBe to the address pointed by newHealthValue

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM