当前位置:网站首页>指针数组和数组指针
指针数组和数组指针
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 指的是这 整个数组 的首地址。
边栏推荐
- C#测试项目中属性的用法
- Outsourcing worked for three years, it was abolished...
- pyqt上手体验
- 列表常用方法
- 2022-08-01 mysql/stoonedb slow SQL-Q18 analysis
- 2022-08-01 mysql/stoonedb慢SQL-Q18分析
- 2022-08-01 安装mysql监控工具phhMyAdmin
- leetcode / anagram in string - some permutation of s1 string is a substring of s2
- Rasa 3.x 学习系列- Rasa - Issues 4873 dispatcher.utter_message 学习笔记
- Safety (2)
猜你喜欢
Flask 报错:WARNING This is a development server. Do not use it in a production deployment
The first time I wrote a programming interview question for Niu Ke: input a string and return the letter with the most occurrences of the string
nacos启动报错,已配置数据库,单机启动
AWR analysis report questions for help: How can SQL be optimized from what aspects?
列表常用方法
Use DBeaver for mysql data backup and recovery
Scheduled tasks for distributed applications in Golang
机器人领域期刊会议汇总
Nanoprobes纳米探针丨Nanogold偶联物的特点和应用
详解最强分布式锁工具:Redisson
随机推荐
数仓:为什么说 ETL 的未来不是 ELT,而是 EL (T)
Flask 报错:WARNING This is a development server. Do not use it in a production deployment
cocos中使用async await异步加载资源
记一次gorm事务及调试解决mysql死锁
29. 删除链表中重复的节点
How to adjust the cross cursor too small, CAD dream drawing calculation skills
EFCore 反向工程
Lombok
国标GB28181协议EasyGBS平台兼容老版本收流端口的功能实现
网络层解析——IP协议、地址管理、路由选择
2022 Henan Youth Training League Game (3)
qt点云配准软件
JVM调优实战
C#测试项目中属性的用法
feign调用不通问题,JSON parse error Illegal character ((CTRL-CHAR, code 31)) only regular white space (r
架构:微服务网关(SIA-Gateway)简介
搭建zabbix监控及邮件报警(超详细教学)
CASE2023
CodeTon Round 2 D. Magical Array
Remember a gorm transaction and debug to solve mysql deadlock