当前位置:网站首页>Accessing a two-dimensional array with a pointer
Accessing a two-dimensional array with a pointer
2022-07-24 06:01:00 【Zonggu22】
List of articles
- Preface
- Access a binary array with a pointer
- Access different forms of two-dimensional arrays through pointers
- ==*a and a It's two completely different concepts :==
- == Why can't you use *p=a==
- There are four ways to access two-dimensional array elements :
- 1、 Access array elements by subscript
- 2、 Access array elements through the first address of the array
- 3、 Access array elements through pointers ( From the perspective of a one-dimensional array “ To view ” Two dimensional array ”)
- 4、 Accessing arrays through pointers ( From the perspective of a two-dimensional array “ To view ” Array )
- Point to a two-dimensional array of strings
- example : Student achievement
- Two dimensional arrays as arguments to functions
- Practice after class
Preface
How to understand two-dimensional arrays ? We understand two-dimensional arrays in a two-dimensional way , It is one-dimensional in storage .
Access a binary array with a pointer
example :int a[3] [4] = {1,3,5,7,9,11,13,15,17,19,21,23};
| a[0] [0] | a[0] [1] | a[0] [2] | a[0] [3] |
|---|---|---|---|
| a[1] [0] | a[1] [1] | a[1] [2] | a[1] [3] |
| a[2] [0] | a[2] [1] | a[2] [2] | a[2] [3] |
We imagine the storage mode of two-dimensional array :3 That's ok 4 Column
| a[0] [0] |
|---|
| a[0] [1] |
| a[0] [2] |
| a[0] [3] |
| a[1] [0] |
| a[1] [1] |
| a[1] [2] |
| a[1] [3] |
| a[2] [0] |
| a[2] [1] |
| a[2] [2] |
| a[2] [3] |
We think the first four are one with a[0] A one bit array of array names , There are four int Elements
The middle four are named a[1] One dimensional array of
The last four are named a[2] One dimensional array of
![[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-xvD4uspk-1649424643293)(C:\Users\17273\AppData\Roaming\Typora\typora-user-images\image-20220408195446474.png)]](/img/79/585f7c39b82ef57fb7ab54f14a65de.png)
Access different forms of two-dimensional arrays through pointers

*a and a It's two completely different concepts :
a Look at this array and think that this array is a one-dimensional array , It adds itself for a while and turns to the next one-dimensional array, that is a+1
*a It refers to a one-dimensional array in which each element is an integer, so it is considered *a+0 It points to an integer , Plus one is the next integer
Why can't you use *p=a
int x,*p;
double y,*q;
p=&x;
q=&y;
p=q;// It's wrong.
//p It refers to the address with four bytes as the storage unit
//q It refers to the address with eight bytes as the storage unit
// The two cannot be equivalent
There are four ways to access two-dimensional array elements :
1、 Access array elements by subscript
int a[3][4]; for (int i=0; i<3; i++) for (int j=0; j<4; j++) a[i][j] = i+j;int a[3][4]; for (int i=0; i<3; i++) for (int j=0; j<4 ;j++) *(*(a+i)+j)=i+j;
2、 Access array elements through the first address of the array
For two-dimensional array elements a[i] [j], “【】” It's actually an index operator , Upcoming element a[i] [j] The storage address of is converted to a[i] + j.
3、 Access array elements through pointers ( From the perspective of a one-dimensional array “ To view ” Two dimensional array ”)
int a[3][4];
int *p=&a[0][0];
for (int i=0; i<3; i++)
for(int j=0; j<4; j++)
*(p++) = i+j; // amount to *p=i+j;p++;
ad locum , The pointer p Is defined as :“ Point to int The pointer to ”, in other words :p The pointer “ Think ” It points to a one-dimensional array , Every element is int Type , share 12 Elements .
4、 Accessing arrays through pointers ( From the perspective of a two-dimensional array “ To view ” Array )
int a[3][4]={
1,3,5,7,9,11,13,15,17,19,21,23};
int (*p)[4];
/* Yes p Understanding of pointer : *p The statement p The pointer to . p What pointer is it pointing to ? Points to a containing 4 One dimensional array of integers */
p=a;
for(int i=0; i<3;i++)
for (int j=0;j < 4;j++)
*(*(p+i)+j)=i+j;
![[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-kABn2WMW-1649424643295)(C:\Users\17273\AppData\Roaming\Typora\typora-user-images\image-20220408203550957.png)]](/img/10/1f72d2e94c21093412e63b0976bab6.png)
After the pointer is defined like this ,== The pointer p And array a Exactly the same “ visual angle ” Look at two-dimensional arrays .== in other words ,a How can I use ,p It can also be used in the same way
// for example : here *(*(p+i)+j) Replace with p[i][j],a[i][j],*(*(a+i)+j) They're all equivalent
Point to a two-dimensional array of strings
char *str[3]={
"Red","Green","Blue"};// Square brackets take precedence over "*" priority
// intend str Is an array with three elements , The array stores pointers to characters
![[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-WhhJAfk3-1649424643296)(C:\Users\17273\AppData\Roaming\Typora\typora-user-images\image-20220408204323760.png)]](/img/8e/65cd1620a88912c6bc88f93340cadc.png)
example : Student achievement
score The array holds 3 A student 4 Your grades
By calling average Function to calculate the average of all students' grades
By calling search The function displays the n A student 4 Grades in this subject .
Ideas :
You need to define a two-dimensional array score, Each row stores a student's 4 Your grades . Yes 3 That's ok , It stores 3 Results of students .
| 65 | 70 | 70 | 60 |
|---|---|---|---|
| 80 | 87 | 90 | 81 |
| 90 | 99 | 100 | 98 |
When writing functions average When calculating the average of all students' grades , It can be used One dimensional array From the perspective of score Array , in other words ,“ Think ” This array has 12 Achievements , Direct summation , Just calculate the average score
When writing functions search According to the first n Of a student 4 When you learn a subject , Then use Two dimensional array From the perspective of score Array , in other words ,“ Think ” This array has 3 That's ok , Each row 4 Elements
void average(float *p,int n)
{
// Look at students' grades in the way of one-dimensional array
float *p_end;
float sum=0,aver;
p_end=p+n;
for ( ; p<p_end;p++)
sum=sum+(*p);
aver=sum/n;
printf(" The average score is :%5.1f\n",aver);
}
void search(float (*p)[4],int n)// It can be written. float p[][4]
{
// Look at students' grades in a two-dimensional array , Perspective and score identical
int i;
printf(" The first %d A student's grade is : ",n);
for (i=0;i<4;i++)
printf("%5.1f",*(*(p+n)+i)); // Can be substituted with p[n][i]
}
int main()
{
float score[3][4]={
{
65,67,70,60},{
80,87,90,81},{
90,99,100,98}};
average(&score[0][0],12); // Can be substituted with score[0],*(score+0)
search(score,2);
return 0;
}
reflection ( If you don't understand , Please step back and look ahead )
(1) It is also passed to the function score Two dimensional array , Why? average Functions and search Different methods are used to transfer parameters of functions ?
average(&score[0][0],12); // Can be substituted with score[0],*(score+0) search(score,2); // Show the second student's grades
Two dimensional arrays as arguments to functions
1、 From the perspective of a one-dimensional array “ To view ” Two dimensional array
![[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-P2KFzj6W-1649424643296)(C:\Users\17273\AppData\Roaming\Typora\typora-user-images\image-20220408210509696.png)]](/img/35/af319daac9edeed0794bc2a3a41410.png)
2、 From the perspective of a two-dimensional array “ To view ” One dimensional array
![[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-hblYxFbm-1649424643297)(C:\Users\17273\AppData\Roaming\Typora\typora-user-images\image-20220408210627250.png)]](/img/a7/b58ccd3b34d0e9a9c433b27ccf74b1.png)
Practice after class
Title Description : Write a inverse function , The function of this function is : Will a 3*4 The values in the two-dimensional array of are stored in reverse order .
Example of program running results
Please enter 3*4 Two dimensional array of :
1 2 3 4
5 6 7 8
9 10 11 12
The result of storage in reverse order is :
12 11 10 9
8 7 6 5
4 3 2 1
Tips :
There are many ways to realize this program , Try two ways to achieve
answer
Method 1 : Look at the program in a two-dimensional array :
![[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-AypqDO38-1649424659790)(C:\Users\17273\AppData\Roaming\Typora\typora-user-images\image-20220408211150882.png)]](/img/f8/154b0c173c51f9c6605b42bd1aa2c1.png)
void inverse(int a[][N],int b[][N])
{
int i,j;
for (i=0;i<M;i++)
for (j=0;j<N;j++)
b[M-i-1][N-j-1]=a[i][j]
}
int main()
{
int a[M][N],b[M][N];
int i,j;
printf(" Please enter %d*%d Two dimensional array of :\n",M,N);
for (i=0;i<M;i++)
for (j=0;j<N;j++)
scanf("%d",&a[i][j]);
inverse(a,b);
printf(" The result of storage in reverse order is :\n");
for (i=0;i<M;i++)
{
for (j=0;j<N;j++)
printf("%d\t",b[i][j]);
}
}
Method 2 : Look at the program in the way of one-dimensional array :
![[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-wbqZDO8I-1649424659791)(C:\Users\17273\AppData\Roaming\Typora\typora-user-images\image-20220408211921472.png)]](/img/89/2a8350bc6f322e19dc81980ba8c9d9.png)
void swap(int *p1,int *p2)
{
int tmp = *p1;
*p1 = *p2;
*p2 = tmp;
}
// With int *a It's a way to pass the reference , Make the array look at in one dimension in the subroutine ,
// in other words , Think of an array as having M*N An element of , Every element is int Array of
void inverse(int *a,int n)
{
int *p,*q;
for (p=a,q=a+n-1;p<=q;p++,q--)
{
swap(p,q);
}
}
int main()
{
int a[M][N];
int i,j;
printf(" Please enter %d * %d Two dimensional array of : \n");
for (i=0;i<M;i++)
for (j=0;j<M;j++)
scanf("%d",&a[i][j]);
inverse(&a[0][0],M*N);
printf(" The result of storage in reverse order is : \n");
for (i=0;i<M;i++)
{
for (j=0;j<N;j++)
printf("%d\t",a[i][j]);
printf("\n");
}
return 0;
}
The program uses M,N Two constants , Such a program has good flexibility .
边栏推荐
- How to quickly connect CRM system and ERP system to realize the automatic flow of business processes
- JUC并发编程基础(4)--线程组和线程优先级
- Deepsort summary
- [activiti] activiti system table description
- systemctl + journalctl
- PDF文本合并
- Signals and systems: Hilbert transform
- [MYCAT] MYCAT installation
- PyTorch 单机多卡分布式训练
- [MYCAT] MYCAT sets up read-write separation
猜你喜欢

JVM系统学习

Chapter 5 neural network

JUC并发编程基础(8)--读写锁

Connect CRM system and effect advertising, help enterprises with precision marketing, and help enterprises with precision marketing

Machine learning (zhouzhihua) Chapter 5 notes on neural network learning
![[MYCAT] MYCAT installation](/img/52/2f77ed64b2ed4e9297acaa8362e194.png)
[MYCAT] MYCAT installation

【USB Host】STM32H7 CubeMX移植带FreeRTOS的USB Host读取U盘,USBH_Process_OS卡死问题,有个值为0xA5A5A5A5

A small problem in labelme to VOC code
![[activiti] personal task](/img/bc/80ac4067f6c58785acb4273f9507ee.png)
[activiti] personal task

Qt新手入门级 计算器加、减、乘、除、应用
随机推荐
In GCC__ attribute__ ((constructor) and__ attribute__ ((destructor)).
Machine learning (Zhou Zhihua) Chapter 4 notes on learning experience of decision tree
树莓派大用处,利用校园网搭建一个校园局域网站
Qt新手入门级 计算器加、减、乘、除、应用
Demo of UDP communication applied to various environments
A small problem in labelme to VOC code
JVM系统学习
LSTM神经网络
读取csv文件的满足条件的行并写入另一个csv中
在网络中添加spp模块中的注意点
STM32标准外设库(标准库)官网下载方法,附带2021最新标准固件库下载链接
QT drawing exception using pure code
【深度学习】手把手教你写“手写数字识别神经网络“,不使用任何框架,纯Numpy
Positional argument after keyword argument
HAL_Delay()延时误差约1ms的问题
day5-jvm
学习率优化策略
[FatFs] migrate FatFs manually and transfer SRAM virtual USB flash disk
Add se channel attention module to the network
PyTorch 单机多卡分布式训练