当前位置:网站首页>二维数组&指针
二维数组&指针
2022-08-09 12:26:00 【进击的李知因】
导言:二维数组在内存中以一维数组的形式存放,所以二维数组可以看成由若干个一维数组构成。
1、行地址&列地址
?行地址:
a &a[0] (指向第0行)
a+i &a[i] (指向第i行)
?列地址:
a[0] &a[0][0] (指向第0行0列)
a[0]+i &a[0][i] (指向第0行i列)
2、行地址?列地址
- 行地址指向一行元素(一个一维数组)
- 列地址指向一个元素
3、一维数组与二维数组的关系
一维数组的地址二维数组的行指针,指向该行元素,如&a[i] a+i
一维数组的元素二维数组的行数组名,指向该行第一个元素,如a[i] &a[i][0]
4、a?a[0]
a是二维数组名,行指针,增1指向下一行
a[0]是行数组名,列指针,增1指向下一列
?行指针
①定义:int (*p)[4];(定义了一个指针变量,指向含有4个元素的整型数组)
②初始化:p=a;或 p=&a[0];
③引用
地址&a[i][j]→ *(p+i)+j
元素a[i][j]→ p[i][j] * (p[i]+j) * (* (p+i)+j) (*(p+i))[j]
?列指针
①定义:int *p;(将二维数组看成m行n列的一维数组)
②初始化:p=*a;或 p=a[0];或 p=&a[0][0];
③引用
地址&a[i][j] → &p[i*n+j]
元素a[i][j] → p[i* n+j] * (p+i*n+j)
示例:输入一个3行4列的二维数组并输出
方法1——二维数组作形参
#include <stdio.h>
#define N 4
void InputArray(int p[][N], int m, int n);
void OutputArray(int p[][N], int m, int n);
int main()
{
int a[3][4];
printf("Input 3*4 numbers:\n");
InputArray(a, 3, 4); /* 向函数传递二维数组的第0行的地址 */
OutputArray(a, 3, 4); /* 向函数传递二维数组的第0行的地址 */
return 0;
}
/* 形参声明为列数已知的二维数组,输入数组元素值 */
void InputArray(int p[][N], int m, int n)
{
int i, j;
for(i = 0; i<m; i++)
for(j = 0; j<n; j++)
scanf("%d", &p[i][j]);
}
/* 形参声明为列数已知的二维数组,输出数组元素值 */
void OutputArray(int p[][N], int m, int n)
{
int i, j;
for(i = 0; i<m; i++)
{
for(j = 0; j<n; j++)
printf("%4d", p[i][j]);
printf("\n");
}
}
方法2——行指针作形参
#include <stdio.h>
#define N 4
void InputArray(int (*p)[N], int m, int n);
void OutputArray(int (*p)[N], int m, int n);
int main()
{
int a[3][4];
printf("Input 3*4 numbers:\n");
InputArray(a, 3, 4); /* 向函数传递二维数组的第0行的地址 */
OutputArray(a, 3, 4); /* 向函数传递二维数组的第0行的地址 */
return 0;
}
/* 形参声明为指向列数已知的二维数组的行指针,输入数组元素值 */
void InputArray(int (*p)[N], int m, int n)
{
int i, j;
for(i = 0; i<m; i++)
for(j = 0; j<n; j++)
scanf("%d", *(p+i)+j);
}
/* 形参声明为指向列数已知的二维数组的行指针,输出数组元素值 */
void OutputArray(int (*p)[N], int m, int n)
{
int i, j;
for(i = 0; i<m; i++)
{
for(j = 0; j<n; j++)
printf("%4d", *(*(p+i)+j));
printf("\n");
}
}
方法3——列指针作形参(推荐,二维列数动态可变)
#include <stdio.h>
void InputArray(int *p, int m, int n);
void OutputArray(int *p, int m, int n);
int main()
{
int a[3][4];
printf("Input 3*4 numbers:\n");
InputArray(*a, 3, 4); /* 向函数传递二维数组的第0行第0列的地址 */
OutputArray(*a, 3, 4); /* 向函数传递二维数组的第0行第0列的地址 */
return 0;
}
/* 形参声明为指向二维数组的列指针,输入数组元素值 */
void InputArray(int *p, int m, int n)
{
int i, j;
for(i = 0; i<m; i++)
for(j = 0; j<n; j++)
scanf("%d", &p[i*n+j]);
}
/* 形参声明为指向二维数组的列指针,输出数组元素值 */
void OutputArray(int *p, int m, int n)
{
int i, j;
for(i = 0; i<m; i++)
{
for(j = 0; j<n; j++)
printf("%4d", p[i*n+j]);
printf("\n");
}
}
边栏推荐
- SQL Server查询优化 (转载非原创)
- 告别手摇织布机的AI时代
- Flutter入门进阶之旅(一)-初识Flutter
- JVM常用监控工具解释以及使用
- [Microservice ~ Remote Call] Integrate RestTemplate, WebClient, Feign
- 一甲子,正青春,CCF创建六十周年庆典在苏州举行
- 在“Extend the Omniverse”比赛中构建用于 3D 世界的工具
- 鹅厂机器狗花式穿越10m梅花桩:前空翻、单桩跳、起身作揖...全程不打一个趔趄...
- Introduction to Flutter advanced trip Dialog&Toast (10)
- 8、IDEA提交代码出现: Fetch failed fatal: Could not read from remote repository
猜你喜欢

2022年非一线IT行业就业前景?

非科班AI小哥火了:他没有ML学位,却拿到DeepMind的offer

26. Pipeline parameter substitution command xargs

Here comes the question: Can I successfully apply for 8G memory on a machine with 4GB physical memory?

新起之秀 DPU,正在掀起数据中心变革!

国产抗新冠口服药每瓶不超300元/ 我国IPv6网络全面建成/ 谷歌入局折叠屏手机...今日更多新鲜事在此...

The grep command Shell regular expressions, the three musketeers

造自己的芯,让谷歌买单!谷歌再度开源 180nm 工艺的芯片

合并两个有序列表

注释、关键字、标识符的区别你知道吗?
随机推荐
h264 protocol
激光熔覆在农机修复强化中的应用及研究方向
腾讯发布第二代四足机器人Max,梅花桩上完成跳跃、空翻
Flutter Getting Started and Advanced Tour (3) Text Widgets
非科班AI小哥火了:他没有ML学位,却拿到DeepMind的offer
用场景定义硬件,英码科技破解“边缘计算”密码
26. Pipeline parameter substitution command xargs
SQL Server查询优化 (转载非原创)
Data Mining-05
Rust从入门到精通04-数据类型
ansible-cmdb friendly display ansible collects host information
十分钟教会你如何使用VitePress搭建及部署个人博客站点
系统提供的堆 VS 手动改写堆
World's 4th mad scientist dies on his 103rd birthday
Extract EventBus encapsulation to base class using annotations
水能自发变成“消毒水”,83岁斯坦福教授:揭示冬天容易得流感的部分原因...
Adalvo acquires its first branded product, Onsolis
基于CAP组件实现补偿事务与幂等性保障
Win10 compiles the x264 library (there are also generated lib files)
【HCIP持续更新】IS-IS协议原理与配置