当前位置: 首页 > news >正文

网站经常被黑重庆网站建设找重庆最佳科技

网站经常被黑,重庆网站建设找重庆最佳科技,网站建设制作方式有哪些,无锡梦燕服饰网站谁做的C中数组和指针的关系#xff08;区别#xff09;详解 本文转自#xff1a;http://c.biancheng.net/view/1472.html 博主在阅读后将文中几个知识点提出来放在前面#xff1a; 没有方括号和下标的数组名称实际上代表数组的起始地址#xff0c;这意味着数组名称实际上就是…C中数组和指针的关系区别详解 本文转自http://c.biancheng.net/view/1472.html 博主在阅读后将文中几个知识点提出来放在前面 没有方括号和下标的数组名称实际上代表数组的起始地址这意味着数组名称实际上就是一个指针。在数学语句中使用指针时它不像常规变量那样工作。向指针添加值时并要解引用时括号非常重要。array[index] 相当于 *(array index)。C 不会对数组执行边界检查。不仅指针符号可以与数组名称一起使用而且下标符号也可以与指针一起使用。可以使用地址运算符来获取数组中单个元素的地址。数组名称和指针变量的唯一区别是不能改变数组名称指向的地址即数组名称可视为一个指针常量。 我们知道没有方括号和下标的数组名称实际上代表数组的起始地址这意味着数组名称实际上就是一个指针。下面程序通过显示与间接运算符一起使用的数组名称来说明这一点。 // This program shows an array name being dereferenced with the * operator. #include iostream using namespace std; int main() {short numbers[] {10, 20, 30, 40, 50};cout The first element of the array is ;cout *numbers endl;return 0; }程序输出结果 The first element of the array is 10numbers 在上面程序中的作用类似于指向数组起始地址的指针所以当 numbers 被解引用时第一个元素被检索出来。那么如何使用间接运算符来检索数组的全部内容呢请记住数组元素是一起存储在内存中的如图 1 所示。 既然 numbers 是 numbers[0] 的地址那么给 numbers 添加值岂不是就可以获得数组中其他元素的地址了这样想当然很有道理但是这里有一个知识点非常重要即在数学语句中使用指针时它不像常规变量那样工作。 在 C 中当给一个指针添加一个值的时候实际上添加的值是把这个值乘以指针引用的数据类型的大小。换句话说如果给 numbers 加 1实际上就是给 numbers 加上 1 X sizeof(short)如果给 numbers 加 2实际上就是给 numbers 2 X sizeof(short)以此类推。在 PC 上这意味着以下说法是真实的因为 short短整数通常使用 2 个字节 *(numbers 1)是指地址 numbers 1X2 处的值。*(numbers 2)是指地址 numbers 2X2 处的值。*(numbers 3)是指地址numbers 3X2 处的值。 以此类推。 这种自动转换意味着数组中的元素可以通过使用其下标或通过将下标添加到指向数组的指针来检索。既然表达式 *numbers 与 *(numbers 0) 相同它可以检索数组中的第一个元素那么*(numbers 1) 就可以检索第二个元素。同样*(numbers2) 即可检索第三个元素以此类推。图 2 显示了下标表示法和指针表示法的等价性。 注意向指针添加值时括号非常重要。* 运算符优先于 运算符所以表达式 *numbers 1 不等于 *(numbers 1)。表达式 *numbers 1 的意思是将数组的第一个元素的内容加 1 而 *(numbers 1) 则是先给 numbers 加 1然后对其进行解引用。 下面的程序使用指针符号显示了被访问数组的整个内容 //This program processes an array using pointer notation. #include iostream using namespace std; int main() {const int SIZE 5; // Size of the arrayint numbers[SIZE]; // Array of integers// Get values to store in the array// Use pointer notation instead of subscriptscout Enter SIZE numbers: ;for (int count 0; count SIZE; count)cin *(numbers count);// Display the values in the array// Use pointer notation instead of subscriptscout Here are the numbers you entered:\n;for (int count 0; count SIZE; count)cout * (numbers count) ;cout endl;return 0; }程序输出结果 Enter 5 numbers: 5 10 15 20 25 Here are the numbers you entered: 5 10 15 20 25在使用数组时请记住一个规则即**array[index] 相当于 *(array index) **。 另外请注意C 不会对数组执行边界检查。当使用指针遍历一个数组时有可能会给指针一个越出数组边界的地址。 要理解数组名称和指针之间的密切关系请看下面的程序。它定义了一个 double 数组和一个 double 指针该指针分配了数组的起始地址。随后不仅指针符号可以与数组名称一起使用而且下标符号也可以与指针一起使用。 // This program uses subscript notation with a pointer // variable and pointer notation with an array name. #include iostream #include iomanip using namespace std; int main() {const int NUM_COINS 5;double coins[NUM_COINS] {0.05, 0.1, 0.25, 0.5, 1.0};double *doublePtr; // Pointer to a double// Assign the address of the coins array to doublePtrdoublePtr coins;// Display the contents of the coins array// Use subscripts with the pointer!cout setprecision (2);cout Here are the values in the coins array:\n;for (int count 0; count NUM_COINS; count)cout doublePtr [count] ;// Display the contents of the coins array again, but this time use pointer notation with the array name!cout \nAnd here they are again:\n;for (int count 0; count NUM_COINS; count)cout *(coins count) ;cout endl;return 0; }程序输出结果 Here are the values in the coins array: 0.05 0.1 0.25 0.5 1 And here they are again: 0.05 0.1 0.25 0.5 1注意当一个数组的地址分配给一个指针时就不需要地址运算符了。由于数组的名称已经是一个地址所以使用 运算符是不正确的。但是可以使用地址运算符来获取数组中单个元素的地址。 例如numbers[1] 得到 numbers[1] 的地址。在程序下面程序中就使用了该技巧。 // This program uses the address of each element in the array. #include iostream #include iomanip using namespace std; int main() {const int NUM_COINS 5;double coins[NUM_COINS] {0.05, 0.1, 0.25, 0.5, 1.0};double *doublePtr; // Pointer to a double//Use the pointer to display the values in the arraycout setprecision (2);cout Here are the values in the coins array:\n;for (int count 0; count NUM_COINS; count){doublePtr coins[count];cout *doublePtr ;}cout endl;return 0; }程序输出结果 Here are the values in the coins array: 0.05 0.1 0.25 0.5 1数组名称和指针变量的唯一区别是不能改变数组名称指向的地址。例如假定存在以下定义 double readings[20], totals[20]; double *dptr;那么以下语句是合法的 dptr readings; // 使 dptr 指向 readings dptr totals; // 使 dptr 指向 totals但是以下语句则是非法的 readings totals; // 非法不能改变 readings totals dptr; // 非法不能改变 totals数组名称是指针常量。不能让它们指向除了它们所代表的数组之外的任何东西。
http://www.yutouwan.com/news/488489/

相关文章:

  • 郴州市建设网站中建八局第一建设有限公司资质
  • 网站建设报价合肥建设工程招聘信息网站
  • 柳州网站建设优化推广今天特大军事新闻
  • 池州专业网站建设公司深圳西乡网站建设公司
  • 中色十二冶金建设有限公司网站企业网站如何优化
  • 三类医疗器械成都网站建设seo优化
  • 小红书推广运营关键词排名优化免费
  • 怎样做网站系统拓者吧室内设计吧官网
  • 长尾关键词挖掘爱站网潍坊网站建设方案
  • 松岗网站设计做家教中介网站赚钱吗?
  • 做橙光游戏的网站wordpress怎么上传网站
  • 网站开发企业gta5买房子网站正在建设
  • 网站导航用什么字体宁波网站建设设计公司信息
  • 重庆网站搭建网站开发的常用流程
  • 网站后台发文章图片链接怎么做华为手机官网入口
  • 响应式网站是什么软件做的织梦模板更新网站
  • 网站禁止ping企业手机网站设计
  • 郑州网站建设专业乐云seo漂亮的网页设计
  • 淘宝网站可以做百度快照吗方正网站制作
  • 亚马逊网站小企业网站建设厂家有哪些
  • 网站程序怎么做佛山大良网站建设招聘
  • 郑州高端网站定制建设海外seo是什么
  • 昆山网站建设昆山html旅游网页设计代码
  • 莒县网站设计分类信息的网站排名怎么做
  • 网站响应方案工信部做网站认证吗
  • 江西合创建设工程有限公司 网站怎么制定wordpress文章的页面
  • 广州 建 网站网站维护方法
  • 上饶哪有做网站的公司?广州白云区123号
  • 做网站能赚吗网站建设seo优化方案
  • 江西做网站安徽网络优化