当前位置:网站首页>二维数组&指针
二维数组&指针
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");
}
}
边栏推荐
- 水能自发变成“消毒水”,83岁斯坦福教授:揭示冬天容易得流感的部分原因...
- Flutter Getting Started and Advanced Tour (3) Text Widgets
- MySQL principle and optimization of Group By optimization techniques
- 曼城推出可检测情绪的智能围巾,把球迷给整迷惑了
- 用 API Factory 产品生成 API 文档
- 随机快排时间复杂度是N平方?
- WeChat side: what is consistent hashing, usage scenarios, and what problems does it solve?
- 自定义VIEW实现应用内消息提醒上下轮播
- Extract EventBus encapsulation to base class using annotations
- Glory to the Blue Yonder, speeds up the strategic growth
猜你喜欢
告别手摇织布机的AI时代
自定义VIEW实现应用内消息提醒上下轮播
Flutter Getting Started and Advanced Tour (4) Text Input Widget TextField
2022年非一线IT行业就业前景?
ABAP 面试题:如何使用 ABAP 编程语言的 System CALL 接口,直接执行 ABAP 服务器所在操作系统的 shell 命令?
用 API Factory 产品生成 API 文档
Here comes the question: Can I successfully apply for 8G memory on a machine with 4GB physical memory?
无需精子卵子子宫体外培育胚胎,Cell论文作者这番话让网友们炸了
无重复字符的最长子串
国产抗新冠口服药每瓶不超300元/ 我国IPv6网络全面建成/ 谷歌入局折叠屏手机...今日更多新鲜事在此...
随机推荐
Flutter入门进阶之旅(八)Button Widget
一甲子,正青春,CCF创建六十周年庆典在苏州举行
位图与位运算
Flutter Getting Started and Advanced Tour (3) Text Widgets
告别手摇织布机的AI时代
苹果Meta都在冲的Pancake技术,中国VR团队YVR竟抢先交出产品答卷
世界第4疯狂的科学家,在103岁生日那天去世了
Flutter入门进阶之旅(六)Layout Widget
The latest interview summary in 20022 brought by Ali senior engineer is too fragrant
Rust from entry to proficient 04 - data types
Report: The number of students who want to learn AI has increased by 200%, and there are not enough teachers
SQL Server查询优化 (转载非原创)
OOM排查和处理
张朝阳对话俞敏洪:一边是手推物理公式,一边是古诗信手拈来
About the handling of variable parameters in the Retrofit network request URL
中断系统结构及中断控制详解
Intranet penetration tool ngrok usage tutorial
Customize VIEW to realize in-app message reminder to rotate up and down
大佬们,请教一下,我看官方文档中,sqlserver cdc只支持2012版之后的,对于sqlser
荣耀携手Blue Yonder,加快企业战略增长