我是否正确理解指针?C++; 我正在学习C++中的指针,我读过一篇文章,我想我理解了,虽然我只是想对我写的伪代码进行一些澄清。 int someGameHealthAddress = 1693; int healthIWantItToBe = 20; int * newHealthValue; newHealthValue = someGameHealthAddress; *newHealthValue = healthIWantItToBe;

我是否正确理解指针?C++; 我正在学习C++中的指针,我读过一篇文章,我想我理解了,虽然我只是想对我写的伪代码进行一些澄清。 int someGameHealthAddress = 1693; int healthIWantItToBe = 20; int * newHealthValue; newHealthValue = someGameHealthAddress; *newHealthValue = healthIWantItToBe;,c++,pointers,C++,Pointers,那么上面的说法正确吗?喜欢它的工作方式吗 编辑:谢谢大家的回答,很高兴我现在记下来了。你帮了大忙:) EDIT2:我很自豪我现在掌握了这个窍门。从表面上看,很多人都很难理解指针。几乎。为了获得指向变量的指针,您需要运算符和的“地址”: // Set newHealthValue to point to someGameHealth newHealthValue = &someGameHealth; newHealthValue = &someGameHealthAddress;

那么上面的说法正确吗?喜欢它的工作方式吗

编辑:谢谢大家的回答,很高兴我现在记下来了。你帮了大忙:)
EDIT2:我很自豪我现在掌握了这个窍门。从表面上看,很多人都很难理解指针。

几乎。为了获得指向变量的指针,您需要运算符
的“地址”:

// Set newHealthValue to point to someGameHealth
newHealthValue = &someGameHealth;
newHealthValue = &someGameHealthAddress;
(我从变量名中删除了“Address”,因为它不是地址。指针现在包含它的地址)


然后,您的最后一行代码将更改
newHealthValue
指向的对象的值,即它将几乎更改
someGameHealth
。为了获得指向变量的指针,您需要运算符
的“地址”:

// Set newHealthValue to point to someGameHealth
newHealthValue = &someGameHealth;
newHealthValue = &someGameHealthAddress;
(我从变量名中删除了“Address”,因为它不是地址。指针现在包含它的地址)


然后,您的最后一行代码将更改
newHealthValue
指向的对象的值,即它将更改
someGameHealth

此语句错误:

newHealthValue = someGameHealthAddress;
因为左边是类型指针,右边是整数。你必须确保作业中的类型匹配。要获取
someGameHealthAddress
的地址,请使用
&

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

现在类型匹配,因为右侧是整数地址,因此是指针。

此语句错误:

newHealthValue = someGameHealthAddress;
因为左边是类型指针,右边是整数。你必须确保作业中的类型匹配。要获取
someGameHealthAddress
的地址,请使用
&

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

现在类型匹配,因为右侧是整数地址,因此是指针。

如果
somegamehealthdress
应该是地址,那么您需要将其声明为整数地址。例如:

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
在您的代码中,这一行是错误的:

newHealthValue=someGameHealthAddress

因为它与变量类型不匹配,所以类似于
int*=int


请注意,这可以从和整数类型转换为指针类型,这几乎总是一个错误,因为您几乎永远不知道内存中变量地址的绝对值。通常定位某个对象,然后使用相对偏移。这通常是在进行内存黑客攻击时出现的情况,您的示例似乎就是这样。

如果
someGameHealthAddress
应该是一个地址,那么您需要将其声明为该地址。例如:

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
在您的代码中,这一行是错误的:

newHealthValue=someGameHealthAddress

因为它与变量类型不匹配,所以类似于
int*=int


请注意,这可以从和整数类型转换为指针类型,这几乎总是一个错误,因为您几乎永远不知道内存中变量地址的绝对值。通常定位某个对象,然后使用相对偏移。这通常是在进行内存黑客攻击时出现的情况,您的示例似乎就是这样。

这取决于您是希望指针的值为1693,还是希望指针指向变量someGameHealthAddress的地址

1.将newHealthValue值指定给someGameHealthAddress值

*newHealthValue = someGameHealthAddress; 
  • 指定newHealthValue以指向someGameHealthAddress变量的地址

    *newHealthValue=&someGameHealthAddress

  • 将newHealthValue的地址分配给someGameHealthAddress变量的值

    &newHealthValue=someGameHealthAddress

  • 将newHealthValue的地址分配给someGameHealthAddress变量的地址

    &newHealthValue=&someGameHealthAddress


  • 这取决于您是希望指针的值为1693,还是希望指针指向变量someGameHealthAddress的地址

    1.将newHealthValue值指定给someGameHealthAddress值

    *newHealthValue = someGameHealthAddress; 
    
  • 指定newHealthValue以指向someGameHealthAddress变量的地址

    *newHealthValue=&someGameHealthAddress

  • 将newHealthValue的地址分配给someGameHealthAddress变量的值

    &newHealthValue=someGameHealthAddress

  • 将newHealthValue的地址分配给someGameHealthAddress变量的地址

    &newHealthValue=&someGameHealthAddress


  • *
    &
    c++

    & means "the address off"
    
    ie,&p是指p的地址

    *p表示存储在p中的地址中的值

    为此,p必须给我一个指针(因为p应该保留一个地址)

    这里
    newHealthValue=someGameHealthAddress将给出编译错误。因为someGameHealthAddress是整数,而newHealthValue是整数指针。
    
    int*=int
    是类型未匹配

    您可以使用以下语句存储someGameHealthAddress的地址

    newHealthValue=&someGameHealthAddress

    which means newHealthValue = (address of)someGameHealthAddress
    

    *newHealthValue=healthiwanttobe
    在语法上是正确的,因为它将
    healthiwanttobe
    的值存储到
    newHealthValue
    *
    &
    c++
    中使用的两个运算符

    & means "the address off"
    
    ie,&p是指p的地址

    *p表示存储在p中的地址中的值

    为此,p必须给我一个指针(因为p应该保留一个地址)

    这里
    newHealthValue=someGameHealthAddress将给出编译错误。因为someGameHealthAddress是整数,而newHealthValue是整数指针。
    
    int*=int
    是类型未匹配

    您可以使用以下状态存储someGameHealthAddress的地址