设为首页 加入收藏

TOP

6.2.4 利用指针改变值(1)
2013-10-07 00:48:54 来源: 作者: 【 】 浏览:61
Tags:6.2.4 利用 指针 改变

6.2.4 利用指针改变值(1)

下面是对目前为止已经介绍的关于内存、地址和指针知识的总结:

创建变量时,系统将分配一些内存块来保存它们的值;

每个内存块都有个独一无二的地址;

变量的地址可以利用&variablename语法(&是"取地址"操作符)来检索;

可以把地址赋值给一种称为指针的特殊的变量;

指针的类型必须与由它保存其地址的变量的类型相一致。

接下来你将看到一些真正的好东西。请看下面这段代码:

  1. int a = 456;  
  2. char b = 'Q';  
  3. int *aPointer = &a;  
  4. char *bPointer = &b; 

这会让程序保留4个内存块,两个为变量保留,两个为指针保留。在为变量保留的内存块里,存放着变量的值;在为指针保留的内存块里,存放着指针的值,这些值是其他变量的地址(如图6-12所示)。

知道了某个变量在内存中的地址(通过指针),就可以利用指针访问位于该地址的数据。这需要对指针做解引用(deference)处理--在指针名的前面加上一个星号(*):

  1. std::cout << *aPointer; 

看明白了吗?把整数变量a的地址存储在aPointer指针里之后,*aPointer和变量a将代表同一个值。进一步说,把一个值赋值给*aPointer和把这个值赋值给变量a的效果完全一样(如图6-13所示):

  1. *aPointer = 123; 
 
图6-12 指针也要占用内存,
但存放在指针里的是其他内存块的地址
(顺便说一句,
字符Q在内存里将被存储为该字
符的ASCII值,
而不是这个字母本身)
 
图6-13 指针可以用来改变存
储在内存块里的值。这么做不会改变
指针的值,因为指针的值
是内存块的地址

利用指针来改变值

1.在文本编辑器或IDE里创建一个空白的新文本文档(代码清单6-4)。

  1. // pointers2.cpp - Script 6.4  
  2. #include  

代码清单6-4 为了演示如何通过指针去访问计算机内存里的值,这个程序先创建了一些变量,然后利用指针改变了它们的值

  1. 1   // pointers2.cpp - Script 6.4  
  2. 2     
  3. 3   #include   
  4. 4     
  5. 5   int main() {  
  6. 6     
  7. 7       // Create some variables.  
  8. 8       int a = -12;  
  9. 9       float b = 78.53;  
  10. 10      char c = 'D';  
  11. 11      unsigned long d = 1904856026;  
  12. 12      std::string e = "arglebargle";  
  13. 13        
  14. 14      // Print the values of the variables.  
  15. 15      std::cout << "The value of a is initially " 
  16. 16      << a << "\n";  
  17. 17      std::cout << "The value of b is initially " 
  18. 18      << b << "\n";  
  19. 19      std::cout << "The value of c is initially " 
  20. 20      << c << "\n";  
  21. 21      std::cout << "The value of d is initially " 
  22. 22      << d << "\n";  
  23. 23      std::cout << "The value of e is initially " 
  24. 24      << e << "\n";  
  25. 25        
  26. 26      // Create matching pointers.  
  27. 27      int *aPointer = &a;  
  28. 28      float *bPointer = &b;  
  29. 29      char *cPointer = &c;  
  30. 30      unsigned long *dPointer = &d;  
  31. 31      std::string *ePointer = &e;  
  32. 32        
  33. 33      // Change the values using the pointers.  
  34. 34      *aPointer = 5462;  
  35. 35      *bPointer = -3.143022489;  
  36. 36      *cPointer = 'Z';  
  37. 37      *dPointer = 1604326026;  
  38. 38      *ePointer = "foofarah";  
  39. 39    
  40. 40      // Print the values of the variables again.  
  41. 41      std::cout << "The value of a is now " 
  42. 42      << a << "\n";  
  43. 43      std::cout << "The value of b is now " 
  44. 44      << b << "\n";  
  45. 45      std::cout << "The value of c is now " 
  46. 46      << c << "\n";  
  47. 47      std::cout << "The value of d is now " 
  48. 48      << d << "\n";  
  49. 49      std::cout << "The value of e is now " 
  50. 50      << e << "\n";  
  51. 51        
  52. 52      std::cout << "Press Enter or Return to continue.\n";  
  53. 53      std::cin.get();  
  54. 54    
  55. 55      return 0;  
  56. 56        
  57. 57  } // End of the main() function. 

【责任编辑:云霞 TEL:(010)68476606】

回书目   上一节   下一节

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇6.2.4 利用指针改变值(2) 下一篇6.2.5 指针和数组(1)

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: