当前位置:网站首页>指针数组和数组指针
指针数组和数组指针
2022-08-02 02:32:00 【陈小帅hh】
一、什么是指针数组
一个数组,若其元素均为指针类型数据,称为指针数组,也就是说,指针数组中的每一个元素都存放一个地址,相当于一个指针变量。
int *p[4];
一般一维的指针数组的一般形式为:类型名 *数组名[]
由于[ ]的优先级比“ * ””高,因此先与[4]结合,形成p[4]形式,这显然是数组形式,表示p数组有4个元素,然后再与前面的“ * ””结合,“ * ””表示此数组是指针类型,每个数组元素(相当于一个指针变量)都可以指向一个整形。
例子:
#include <stdio.h>
#include <stdlib.h>
int getmax(int a,int b)
{
return a>b?a:b;
}
int getmin(int a,int b)
{
return a<b?a:b;
}
int getsum(int a,int b)
{
return a+b;
}
int main()
{
int a=10;
int b=20;
int (*pfnc[3])(int a,int b)={
getmax,getmin,getsum};
int ret;
for(int i=0;i<3;i++){
ret=(*pfnc[i])(a,b);
printf("%d\n",ret);
}
}
二、什么是数组指针
数组指针,顾名思义,就是指向数组的指针。
我们是这样定义它的:int(* p)[n] = { }; (n为要定义的个数)
按照优先级运算
()与[ ] 优先级相同,根据结合律,就从左向右运算。
()里是*p,先定义了指针,所以p是个指针,然后后面是[ ],才是数组,即数组指针。它指向了含有n个int类型的数组。
如上图所示,假设定义 int (*p2)[5] = { 0, 0, 0, 0, 0}; 那么p2就指向了这个包含5个int类型的数组了。
( !!!注意:指向数组和指向数组首元素的地址是两码事。p2在内存中指向的是这个数组的首地址,是和数组有关联的,而绝不仅仅是指向数组首元素的地址。)
就比如:
int *p1 = b;
int a[5] = {1, 2, 3, 4, 5};
int *p2 = a;
指针p1和p2就单纯的指向了变量,p1指向了变量b的地址,p2指向的是a数组首元素的地址,而不是指向这个数组,它与整个的数组是无关的。
例子:
#include <stdio.h>
int main()
{
int temp[5] = {
1, 2, 3, 4, 5};
int (*p)[5] = &temp;
int i;
for(i = 0; i < 5; i++)
{
printf("%d\n", *(*p + i)); //*p是首个元素的地址
//或者 printf("%d\n", (*p)[i]);
}
return 0;
}
1)第一行定义了含有5个int类型的数组temp;
2)第二行就定义了一个指向含有5个int类型数组的指针p,并把temp数组的首地址给了指针p。
注意:这里为什么不直接用 int (*p)[5] = temp;呢?
这是因为虽然temp和&temp的值都是相同的,但它们的意义却是不相同的:
*** temp指的是这个数组的 第一个元素 的首地址。
*** &temp 指的是这 整个数组 的首地址。
边栏推荐
- 29. 删除链表中重复的节点
- 2022 NPDP take an examination of how the results?How to query?
- 2022牛客多校三_F G
- Remember a pit for gorm initialization
- 接口测试神器Apifox究竟有多香?
- 2022-08-01 mysql/stoonedb慢SQL-Q18分析
- 【ORB_SLAM2】void Frame::AssignFeaturesToGrid()
- OC和Swift语言的区别
- Good News | AR opens a new model for the textile industry, and ALVA Systems wins another award!
- ofstream,ifstream,fstream read and write files
猜你喜欢
随机推荐
mockjs生成假数据的基本使用
Docker-compose安装mysql
Software testing Interface automation testing Pytest framework encapsulates requests library Encapsulates unified request and multiple base path processing Interface association encapsulation Test cas
OC中成员变量,实例变量和属性之间的区别和联系
AWR分析报告问题求助:SQL如何可以从哪几个方面优化?
EasyGBS平台播放视频时偶尔出现播放失败是什么原因?
记一个gorm初始化的坑
线程的不同状态
Pinduoduo leverages the consumer expo to promote the upgrading of domestic agricultural products brands and keep pace with international high-quality agricultural products
Swift运行时(派发机制)
Analysis of the status quo of digital transformation of manufacturing enterprises
Nanoprobes丨1-巯基-(三甘醇)甲醚功能化金纳米颗粒
FOFAHUB使用测试
How engineers treat open source
Oracle数据类型介绍
How to adjust the cross cursor too small, CAD dream drawing calculation skills
字典常用方法
Talking about the "horizontal, vertical and vertical" development trend of domestic ERP
架构:分布式任务调度系统(SIA-Task)简介
一次SQL优化,数据库查询速度提升 60 倍









