指针和数组
来源:互联网 发布:比较好的淘宝女装店 编辑:程序博客网 时间:2024/06/14 17:59
指针和数组
指针和数组在C语言中有错综复杂的联系。在前面的课程中,你学会了如何声明数组变量:
int anArray[5]; // declare array of 5 integersanArray is actually a pointer that points to the first element of the array! Because the array variable is a pointer, you can dereference it, which returns array element 0:int anArray[5] = { 9, 7, 5, 3, 1 };// dereferencing an array returns the first element (element 0)cout << *anArray; // prints 9!char szName[] = "Jason"; // C-style string (also an array)cout << *szName; // prints 'J'
指针的算术运算
C语言允许你在指针执行整数的加减运算。如果pnptr指向一个整数,pnptr + 1 pnptr后在内存中的地址的下一个整数。pnptr - 1是在pnptr以前的整数地址。
请注意,pnptr + 1不在pnptr返回地址,但下一个对象,pnptr分型。如果pnptr指向一个整数(假设4字节),pnptr + 3意味着在pnptr 3个整数,这是12个地址后pnptr。如果pnptr指向一个char,这始终是1字节,pnptr + 3意味着在pnptr 3字符,这是3个地址后pnptr。
计算一个指针的算术表达式的结果时,编译器总是乘以整数操作数所指对象的大小。这就是所谓的缩放。
下面的程序:
1234567int nValue = 7;int *pnPtr = &nValue; cout << pnPtr << endl;cout << pnPtr+1 << endl;cout << pnPtr+2 << endl;cout << pnPtr+3 << endl;
0 0
- 指针和指针数组
- 指针数组和数组指针
- 指针数组和数组指针
- 指针数组和数组指针
- 指针数组和数组指针
- 数组指针和指针数组
- 指针数组和数组指针
- 数组指针和指针数组
- 指针数组和数组指针
- 指针数组和数组指针
- 数组指针和指针数组
- 指针数组和数组指针
- 指针数组和数组指针
- 数组指针和指针数组
- 数组指针和指针数组
- 指针数组和数组指针
- 指针数组和数组指针
- 数组指针和指针数组
- 【转载】Android中build target,minSdkVersion,targetSdkVersion,maxSdkVersion概念区分
- 线性表----循环队列(顺序表示)的基本操作
- 大数据究竟是什么?一篇文章让你认识并读懂大数据
- 在Windows下如何练习Linux下的vim? (为即将步入IT行业的兄弟和妹子介绍一个工具)
- jqueryAPI学习之add(),addBack(),addClass(),after()学习
- 指针和数组
- HDOJ 3037 Saving Beans
- hive external table partition 关联HDFS数据
- 设定Windows Server 2008 为NTP时间服务器 & 客户端设定
- 笔者的机器
- 整除的尾数(2099)
- 【Leetcode长征系列】Best Time to Buy and Sell Stock
- 各种音频视频编码方法
- Linux基础之一