学习指针示例

时间:2011-04-07 05:18:03

标签: c++ pointers

您好我遇到了一些C ++代码,并且正在尝试了解指针的运行方式。

   int main ()
{
  int firstvalue, secondvalue;
  int * mypointer;

  mypointer = &firstvalue;
  *mypointer = 10;
  mypointer = &secondvalue;
  *mypointer = 20;
  cout << "firstvalue is " << firstvalue << endl;
  cout << "secondvalue is " << secondvalue << endl;
  return 0;
}

我的问题如下:

  1. 宣布int *mypointer;时内存中究竟发生了什么?
  2. mypointer代表什么?
  3. *mypointer代表什么?
  4. *mypointer = 10;内存中发生了什么?

6 个答案:

答案 0 :(得分:3)

  1. int * mypointer时内存中究竟发生了什么;宣布?
  2. 从堆栈分配足够的内存来存储内存地址。这将是32位或64位,具体取决于您的操作系统。

    1. mypointer代表什么?
    2. mypointer是堆栈中包含内存地址的变量。

      1. mypointer代表什么?
      2. *mypointermypointer指向的实际内存位置。

        1. 当* mypointer = 10时;内存会发生什么?
        2. 10存储在mypointer指向的内存位置。例如,如果mypointer包含内存地址0x00004000,则值10将存储在内存中的该位置。

          您的评论示例:

          int main ()
          {
            int firstvalue, secondvalue;   // declares two integer variables on the stack
            int * mypointer;               // declares a pointer-to-int variable on the stack
          
            mypointer = &firstvalue;       // sets mypointer to the address of firstvalue
            *mypointer = 10;               // sets the location pointed to by mypointer to 10.
                                              In this case same as firstvalue = 10; because
                                              mypointer contains the address of firstvalue
            mypointer = &secondvalue;      // sets mypointer to the address of secondvalue
            *mypointer = 20;               // sets the location pointed to by mypointer to 10. 
                                              In this case same as secondvalue = 20; because 
                                              mypointer contains the address of secondvalue
            cout << "firstvalue is " << firstvalue << endl;
            cout << "secondvalue is " << secondvalue << endl;
            return 0;
          }
          

          试试这段代码,看看是否有帮助:

          int main ()
          {
            int firstvalue, secondvalue;   
            int * mypointer;               
          
            cout << "firstvalue is " << firstvalue << endl;
            cout << "secondvalue is " << secondvalue << endl;
            cout << "mypointer is pointing to " << mypointer << endl;
          
            mypointer = &firstvalue;       
          
            cout << "firstvalue is " << firstvalue << endl;
            cout << "secondvalue is " << secondvalue << endl;
            cout << "mypointer is pointing to " << mypointer << endl;
          
            *mypointer = 10;               
          
            cout << "firstvalue is " << firstvalue << endl;
            cout << "secondvalue is " << secondvalue << endl;
            cout << "mypointer is pointing to " << mypointer << endl;
          
            mypointer = &secondvalue;      
          
            cout << "firstvalue is " << firstvalue << endl;
            cout << "secondvalue is " << secondvalue << endl;
            cout << "mypointer is pointing to " << mypointer << endl;
          
            *mypointer = 20;               
          
            cout << "firstvalue is " << firstvalue << endl;
            cout << "secondvalue is " << secondvalue << endl;
            cout << "mypointer is pointing to " << mypointer << endl;
          
            return 0;
          }
          

答案 1 :(得分:2)

pointers

答案 2 :(得分:1)

  1. 这将创建一个指向内存中某个位置的指针。通过在int *中定义它,我们说当我们取消引用这个指针时,字节应该被转换为int。
  2. myPointer指向内存中的某个位置。如果您自己访问myPointer,它将为您提供存储它指向的数据的地址。
  3. * myPointer取消引用该点以提供指针指向的值。
  4. * myPointer = 10:取值10并将其存储在myPointer指向的内存中。

答案 3 :(得分:1)

int * mypointer;
它是一个声明..它告诉编译器我们将使用mypointer作为整数指针变量。

mypointer =&amp; firstvalue;
这里firstvalue的地址存储在mypointer中。

* mypointer = 10;
考虑mypointer指向地址位置4000.现在值10存储在地址位置4000中。

任何其他疑问请与我沟通

答案 4 :(得分:1)

  

int * mypointer时内存中究竟发生了什么;宣布?

声明

mypointer保存int的地址。因此,保存整数地址所需的字节数被分配给mypointer

  

mypointer代表什么?

它是一个可以保存整数变量地址的变量。

  

* mypointer代表什么?

它取消引用指针。获取地址位置mypointer的值。

  

当* mypointer = 10时;记忆中会发生什么?

10位置分配值mypointer

答案 5 :(得分:1)

  1. 当你声明int *mypointer时,它声明一个指针类型(一个长整数),它可以包含一个整数的内存地址。
  2. mypointer表示整数的地址,其方式与街道地址代表街道上的房屋非常相似。
  3. *mypointer取消引用地址并指向int的实际值。这与地址指向的实际房屋类似。
  4. 在内存中,mypointer指向的内存位置的值将更新。