当前位置:网站首页>二维数组&指针
二维数组&指针
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");
}
}
边栏推荐
- Flutter入门进阶之旅(一)-初识Flutter
- Flutter Getting Started and Advanced Tour (1) - Getting to Know Flutter
- WeChat payment development process
- 史上最猛“员工”,疯狂吐槽亿万富翁老板小扎:那么有钱,还总穿着同样的衣服!...
- 生成上传密钥和密钥库
- Two minutes recording can pass by second language!The volcano how to practice and become voice tone reproduction technology?
- 工作任务统计
- Flutter入门进阶之旅(七)GestureDetector
- 链表噩梦之一?5000多字带你弄清它的来龙去脉
- 2022年非一线IT行业就业前景?
猜你喜欢
Fragment中嵌套ViewPager数据空白页异常问题分析
Too much volume... Tencent was asked on the side that the memory was full, what would happen?
Scala 高阶(七):集合内容汇总(上篇)
AI basketball referee, walking is special, ask harden care don't care
透明tune proxy
win10编译x264库(也有生成好的lib文件)
The latest interview summary in 20022 brought by Ali senior engineer is too fragrant
ABAP interview questions: how to use the System CALL interface of the ABAP programming language, direct execution ABAP server operating System's shell command?
Flutter Getting Started and Advanced Tour (4) Text Input Widget TextField
数字化转型之支撑保障单元
随机推荐
ABP中的数据过滤器 (转载非原创)
无需精子卵子子宫体外培育胚胎,Cell论文作者这番话让网友们炸了
Compensation transaction and idempotency guarantee based on CAP components
Two minutes recording can pass by second language!The volcano how to practice and become voice tone reproduction technology?
win10编译x264库(也有生成好的lib文件)
罗振宇折戟创业板/ B站回应HR称用户是Loser/ 腾讯罗技年内合推云游戏掌机...今日更多新鲜事在此...
The latest interview summary in 20022 brought by Ali senior engineer is too fragrant
WeChat Mini Program Payment and Refund Overall Process
技术分享 | 接口自动化测试如何处理 Header cookie
在“Extend the Omniverse”比赛中构建用于 3D 世界的工具
Flutter Getting Started and Advanced Tour (3) Text Widgets
ABAP 报表中如何以二进制方式上传本地文件试读版
保存Simulink仿真模型为图片或者PDF的方法
JVM内存泄漏和内存溢出的原因
关于Retrofit网络请求URL中含有可变参数的处理
World's 4th mad scientist dies on his 103rd birthday
卷积神经网络表征可视化研究综述(1)
Intranet penetration tool ngrok usage tutorial
工作任务统计
水能自发变成“消毒水”,83岁斯坦福教授:揭示冬天容易得流感的部分原因...