当前位置:网站首页>二维数组&指针
二维数组&指针
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");
}
}
边栏推荐
- 荣耀携手Blue Yonder,加快企业战略增长
- ERP不规范,同事两行泪 (转载非原创)
- Simple encapsulation of glide tool class
- 鹅厂机器狗花式穿越10m梅花桩:前空翻、单桩跳、起身作揖...全程不打一个趔趄...
- [Microservice ~ Remote Call] Integrate RestTemplate, WebClient, Feign
- Here comes the question: Can I successfully apply for 8G memory on a machine with 4GB physical memory?
- 无重复字符的最长子串
- Resolved IndentationError: unindent does not match any oute r indentation Level
- Simple understanding of ThreadLocal
- 随机快排时间复杂度是N平方?
猜你喜欢
随机推荐
JVM内存泄漏和内存溢出的原因
1小时直播招募令:行业大咖干货分享,企业报名开启丨量子位·视点
MySQL 原理与优化,Group By 优化 技巧
Rust from entry to proficient 04 - data types
Resolved IndentationError: unindent does not match any oute r indentation Level
报告:想学AI的学生数量已涨200%,老师都不够用了
AI basketball referee, walking is special, ask harden care don't care
[Microservice ~ Remote Call] Integrate RestTemplate, WebClient, Feign
K个结点的组内逆序调整
保存Simulink仿真模型为图片或者PDF的方法
Two minutes recording can pass by second language!The volcano how to practice and become voice tone reproduction technology?
Extract EventBus encapsulation to base class using annotations
水能自发变成“消毒水”,83岁斯坦福教授:揭示冬天容易得流感的部分原因...
无重复字符的最长子串
Flutter入门进阶之旅(四)文本输入Widget TextField
国产抗新冠口服药每瓶不超300元/ 我国IPv6网络全面建成/ 谷歌入局折叠屏手机...今日更多新鲜事在此...
中科院打脸谷歌:普通电脑追上量子优越性,几小时搞定原本要一万年的计算...
ansible-cmdb友好展示ansible收集主机信息
Flutter入门进阶之旅(十)Dialog&Toast
MySQL principle and optimization of Group By optimization techniques









