[{"createTime":1735734952000,"id":1,"img":"hwy_ms_500_252.jpeg","link":"https://activity.huaweicloud.com/cps.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=V1g3MDY4NTY=&utm_medium=cps&utm_campaign=201905","name":"华为云秒杀","status":9,"txt":"华为云38元秒杀","type":1,"updateTime":1735747411000,"userId":3},{"createTime":1736173885000,"id":2,"img":"txy_480_300.png","link":"https://cloud.tencent.com/act/cps/redirect?redirect=1077&cps_key=edb15096bfff75effaaa8c8bb66138bd&from=console","name":"腾讯云秒杀","status":9,"txt":"腾讯云限量秒杀","type":1,"updateTime":1736173885000,"userId":3},{"createTime":1736177492000,"id":3,"img":"aly_251_140.png","link":"https://www.aliyun.com/minisite/goods?userCode=pwp8kmv3","memo":"","name":"阿里云","status":9,"txt":"阿里云2折起","type":1,"updateTime":1736177492000,"userId":3},{"createTime":1735660800000,"id":4,"img":"vultr_560_300.png","link":"https://www.vultr.com/?ref=9603742-8H","name":"Vultr","status":9,"txt":"Vultr送$100","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":5,"img":"jdy_663_320.jpg","link":"https://3.cn/2ay1-e5t","name":"京东云","status":9,"txt":"京东云特惠专区","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":6,"img":"new_ads.png","link":"https://www.iodraw.com/ads","name":"发布广告","status":9,"txt":"发布广告","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":7,"img":"yun_910_50.png","link":"https://activity.huaweicloud.com/discount_area_v5/index.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=aXhpYW95YW5nOA===&utm_medium=cps&utm_campaign=201905","name":"底部","status":9,"txt":"高性能云服务器2折起","type":2,"updateTime":1735660800000,"userId":3}]
<>指针访问数组元素
指针的运算本身就是适合对连续的数据进行处理,而数组正好满足这一要求。所以这一节研究如何使用指针访问数组元素。
首先我们举个例子
int a[10],*p;//定义一个数组和一个指针变量p p=a;//或者p=&a[0];这两句话的意思都是将数组a的首地址赋值给p
<>等效形式
*p就是a[0],是寻址运算符,就是找到p变量中保存的地址里面的内容,即数组a的第一个元素。
同理(p+i)就是找到对应数组中的第i个元素a[i]。
与此同时 *(a+i)和p[i]也都代表相同的含义。
总结一下便是 *(指针+i) = *(数组名+i) = 数组名[i] = 指针变量名[i]
数组名a是数组的首地址,是一个常量,所以不能进行自加自减运算。
<>指针数组
数组的每个元素都是指针变量,这个数组就是指针数组。
语法形式
数据类型 *数组名[下标表达式];
下标表达式指出数组元素的个数,数据类型表示指针的类型,数组名是指针数组的名称也是数组的首地址。
例例用指针数组输出单位矩阵
#include<iostream> using namespace std; int main() { int line1[] = { 1,0,0 };
int line2[] = { 0,1,0 }; int line3[] = { 0,0,1 }; int* pline[3] = { line1,line2,
line3}; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) cout << pline[
i][j] << " "; cout << endl; } return 0; }
将三个一维数组的数组名作为指针数组的元素,完成了指针数组的初始化,每个元素分别指向矩阵的一行。
pline[i][j]并不是二维数组而是一维指针数组,前面的pline[i]是指针名,后面的[j]就是对应的元素。(具体看上面的等价方式)。在内存中的存储方式也是不相同的二维数组的元素在内存中是连续的一行接一行,而指针数组在内存中是每行的元素是连续的,但是行与行之间没有关系。
虽然二维数组和指针数组存在本质的差异,但是还是可以通过指针数组对二维数组进行访问。
例 二维数组举例
#include <iostream> using namespace std; int main() { int array2[3][3]= { { 11,
12, 13 }, { 21, 22, 23 }, { 31, 32, 33 } }; for(int i = 0; i < 3; i++) { for(int
j= 0; j < 3; j++) cout << *(*(array2 + i) + j) << " "; //逐个输出二维数组第i行元素值 cout <<
endl; } return 0; }
通过数组元素的地址可以输出二维数组元素,使用*(*(arry2+i)+j)这就是arry2数组的第i行第j列元素,对应下标是arry2[i][j].