简体   繁体   English

我是否正确理解指针? C ++

[英]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. 我正在学习C ++中的指针,并且已经阅读了有关它的文章,并且我想我理解了它,尽管我只是想澄清我编写的伪代码。

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. 您提供了很大的帮助:) EDIT2:我很自豪,现在我已经掌握了这一点。 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. 如果someGameHealthAddress应该是一个地址,则需要这样声明它。 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 . 因为它与变量类型不匹配,所以就像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 . 然后,您的最后一行代码将更改newHealthValue指向的对象的值,即它将更改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 & : 要获取someGameHealthAddress的地址,请使用&

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 取决于您是否希望指针的值为1693,还是希望指针指向变量some​​GameHealthAddress的地址

1.assigns newHealthValue value to someGameHealthAddress value 1.将newHealthValue值分配给someGameHealthAddress值

*newHealthValue = someGameHealthAddress; 
  1. assigns the newHealthValue to point to the address of the someGameHealthAddress variable 分配newHealthValue指向someGameHealthAddress变量的地址

    *newHealthValue = &someGameHealthAddress; * newHealthValue =&someGameHealthAddress;

  2. assigns the address of newHealthValue to the value of the someGameHealthAddress variable 将newHealthValue的地址分配给someGameHealthAddress变量的值

    &newHealthValue = someGameHealthAddress; &newHealthValue = someGameHealthAddress;

  3. assigns the address of the newHealthValue to the address of the someGameHealthAddress variable 将newHealthValue的地址分配给someGameHealthAddress变量的地址

    &newHealthValue = &someGameHealthAddress; &newHealthValue =&someGameHealthAddress;

* and & are two operators used in c++ *&c++使用的两个运算符

& means "the address off"

ie, &p means address of p . 即&p表示p的地址。

* means "value in the location"

*p means value in the address stored in p. * p表示存储在p中的地址中的值。

For that p must me a pointer.(because p should keep an address). 为此,p必须是一个指针。(因为p应该保留一个地址)。

Here newHealthValue = someGameHealthAddress; 这里newHealthValue = someGameHealthAddress; will give compilation error.because someGameHealthAddress is an integer and newHealthValue is integer pointer. 将给出编译错误。因为someGameHealthAddress是整数,而newHealthValue是整数指针。 int*=int is type miss-match int*=int是类型不匹配

You can store the address of someGameHealthAddress using the following statement 您可以使用以下语句存储someGameHealthAddress的地址

newHealthValue = &someGameHealthAddress ; 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 在语法上是正确的,因为它存储的值healthIWantItToBe被指向的地址newHealthValue

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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