Pointer(2) – pointer in array

Summary of insights into programming learning,
Record every moment of struggle,
I hope it can help you who are also working hard!
If there are any shortcomings, please correct me!
Learn and communicate together!
Welcome everyone → Like + Collection? + Leave a message?
Since we have chosen a distant place, we only care about the journey through wind and rain!

Table of Contents

Introduction:

1. Pointers in one-dimensional arrays:

Question 1: One-dimensional integer array a has 10 elements. Enter 10 integers and store them in array a.

Method 1: Obtain the pointer of the element through the address operator.

Method 2: Find the pointer of the element by adding i to the pointer a of element 0 of the array.

Method 3: Use pointer variables pointing to array elements.

Question 2: One-dimensional integer array a stores 10 data, and outputs all the data in the array.

Method 1: Use the subscript of the array element to access the array element.

Method 2: Access the array elements through the first address of the array (array name).

Method 3: Access array elements through pointer variables pointing to array elements.

2. Pointers in two-dimensional arrays:

1. Access array elements using two-dimensional array element pointers

Question 1: There is an integer array with 3 rows and 4 columns, which stores 12 data and outputs all the data of the two-dimensional array.

Method 1: Access the elements of the two-dimensional array through the first address (x[i]) of each row of the two-dimensional array.

Method 2: Access the two-dimensional array elements through pointer variables pointing to the two-dimensional array elements.

2. Use the row pointer of the two-dimensional array to access the array elements

Method 1: Access the elements of the two-dimensional array through the two-dimensional array name, x.

Method 2: Access two-dimensional array elements through pointer variables pointing to two-dimensional array rows.


Introduction:

Since array elements are stored continuously in memory, if you know the pointer of one element, you can obtain the pointers of other elements through addition and subtraction operations, and then access the array elements. C language stipulates that one-dimensional array name is a pointer constant, representing the starting address of the array in memory, precisely the pointer to element 0 of the array.

1. Pointers in one-dimensional arrays:

Here are examples to understand pointers in depth

Topic 1: One-dimensional integer array a, has 10 elements, input 10 The integers are stored in array a.

Method 1: Obtain the pointer of the element through the address operator.

#include<stdio.h>
int main(){
int a[10],i;
for(i=0;i<10;i + + )
scanf("%d", & amp;a[i]); /* & amp;a[i] is the pointer to array element a[i]*/
}

Method 2: Find the pointer of the element by adding i to pointer a of array 0 element.

#include<stdio.h>
int main(){
int a[10],i;
for(i=0;i<10;i + + )
scanf("%d",a + i); /*a + i is a pointer to array element a[i]*/
}

Method 3: Use pointer variables pointing to array elements.

#include<stdio.h>
int main(){
int a[10],*p;
p=a; /*or p= & amp;a[0]; pointer variable p points to array element a[0]*/
for(;p<(a + 10);p + + )
scanf("%d",p);/*During the loop, the value of p is the pointer of a[0]~a[9]*/
}

or

#include<stdio.h>
int main(){
int a[10],*p;
p= &a[0];
for(i=0;i<10;i + + )
scanf("%d",p + i);/*The value of p remains unchanged during the loop, and p + i is the pointer to the array element a[i]*/
}

Question 2: One-dimensional integer array a, stores 10 data, outputs all the data in the array All data.

Method 1: Use the subscript of the array element to access the array element.

#include<stdio.h>
int main(){
int a[10]={0,1,2,3,4,5,6,7,8,9},i;
for(i=0;i<10;i + + )
printf("=",a[i]);/*Subscript method*/
}

Method 2: Access the array elements through the first address (array name) of the array.

#include<stdio.h>
int main(){
int a[10]={0,1,2,3,4,5,6,7,8,9},i;
for(i=0;i<10;i + + )
printf("=",*(a + i));/*pointer method*/
}

Method 3: Access array elements through pointer variables pointing to array elements.

#include<stdio.h>
int main(){
int a[10]={0,1,2,3,4,5,6,7,8,9},*p;
p= &a[0];
for(;p<(a + 10);p + + )
printf("=",*p);/*pointer method*/
}

or

#include<stdio.h>
int main(){
int a[10]={0,1,2,3,4,5,6,7,8,9},*p,i;
p=a;
for(i=0;i<10;i + + )
printf("=",*p + i);//or p[i]
}

When using pointer variables, pay attention to the following points.
(1) Pointer variables can increase and decrease by themselves, but array names cannot increase and decrease by themselves.
For example, the third method is to use the pointer variable p to point to the array element, and use p + to keep the value of p changing. This is legal. What if we do not use p + + but use at +? No. Because a is the name of the array and the first address of the array, its value is fixed during the running of the program and is a constant.

(2) Pay attention to the current value of the pointer variable.

For example, in method three, the pointer variable p keeps changing and points to each element of the array in turn. When the loop ends, where does the pointer p point? At this time, the value of p is not & a[], but & a[10], which points to a unit outside the array.

(3) Be careful to prevent subscripts from going out of bounds.

For example, in method three, if array a executes *(p + 10)=18, 18 is not assigned to the array element, but 18 is assigned to the next storage unit of element a[9]. If this storage unit originally The data is important data to be used by the program, so the above assignment operation will change this important data to 18, which may cause the program to produce completely wrong results.

2. Pointers in two-dimensional arrays:

A two-dimensional array is an extension of a one-dimensional array. If the element of the one-dimensional array itself is a one-dimensional array, it forms a two-dimensional array. The pointers in the two-dimensional array are more complicated than the one-dimensional array. .

There are two types of pointers in a two-dimensional array: 1. Element pointer, pointer of integer element x[i][j] & amp;x[i][j] or x[i] + j. 2. Row pointer, pointer of x[0]~x[2] & x[i] or x + i, the array name x is the pointer of x[0], which is the row pointer. There are also two methods for accessing two-dimensional array elements: 1. Subscript method; 2. Pointer method. The subscript method will not be discussed in detail here.

Here we also use examples to understand pointers

1. Use two-dimensional array element pointers to access array elements

Method 1: Access the elements of the two-dimensional array through the first address (x[i]) of each row of the two-dimensional array Access elements of a two-dimensional array.

#include<stdio.h>
int main(){
int x[3][4]={<!-- -->{0,1,2,3},{4,5,6,7},{8,9,10,11}},i, j;
for(i=0;i<3;i + + ){
for(j=0;j<4;j + + ){
printf("=",*(x[i] + j)); /* x[i] + j is the pointer of element x[iJ[j]*/
}
printf("\
");
}
}

Method 2: Access two-dimensional array elements through pointer variables pointing to two-dimensional array elements.

#include<stdio.h>
int main(){
int x[3][4]={<!-- -->{0,1,2,3},{4,5,6,7},{8,9,10,11}},i, j,*p;
p= & amp;x[0][0];/*or p=x[0];p is an integer pointer variable and cannot be written: p=x;*/
for(i=0;i<3;i + + ){
for(j=0;j<4;j + + ){
printf("=",*p + + ); /* *p + + is equivalent to *(p + + ) */
}
printf("\
");
}
}

or

#include<stdio.h>
int main(){
int x[3][4]={<!-- -->{0,1,2,3},{4,5,6,7},{8,9,10,11}},i, j,*p;
p=x[0];
for(i=0;i<3;i + + ){
for(j=0;j<4;j + + ){
printf("=",*(p + i*4 + j)); /*p remains unchanged, p + i*4 + j is the pointer of x[i][j]*/
}
printf("\
");
}
}

2. Use the row pointer of the two-dimensional array to access the array elements

& amp; Such a pointer should be stored in a pointer variable pointing to a row, which is defined as follows:
Type (*pointer variable name)[m];
The parentheses cannot be omitted, and m is an integer constant. Define a pointer variable pointing to the entire one-dimensional array consisting of m elements.

Combined with the access method of one-dimensional array elements, it can be seen that x + i is the pointer of element x[i]: *(x + i) is x[i], x[i] + j is the pointer of element x[i][j] Pointer, so *(x + i) + j is the pointer of element x[i][j], *(*(x + i) + j) is the array element x[i][j]. Remember, adding * in front of the row pointer is the pointer to the 0 element of the row.

Example of using row pointers to output all the data of the two-dimensional array in question 1

Method One: Access the elements of the two-dimensional array through the two-dimensional array name, x.

#include<stdio.h>
int main(){
int x[3][4]={<!-- -->{0,1,2,3},{4,5,6,7},{8,9,10,11}},i, j;
for(i=0;i<3;i + + ){
for(j=0;j<4;j + + ){
printf("=",*(*(x + i) + j));
}
printf("\
");
}
}

Method 2: Access two-dimensional array elements through pointer variables pointing to two-dimensional array rows.

#include<stdio.h>
int main(){
int x[3][4]={<!-- -->{0,1,2,3},{4,5,6,7},{8,9,10,11}},i, j;
int (*p)[4];/*Definition p is a pointer variable pointing to a two-dimensional array row*/
p= & amp;x[0]; /*or p=x;p points to line 0 and cannot be written: p=x[0];*/
for(i=0;i<3;i + + ){
for(j=0;j<4;j + + ){
printf("=",*(*p + j));/* *p is the pointer to element 0 in the row pointed to by p */
}
p + + ;/* p + + makes p point to the next line */
printf("\
");
}
}

3. Summary:

Distinguish between pointers and the memory space accessed by pointers. The pointer and the starting address of the array pointed by the pointer only point to it, but the memory space pointed by the pointer is accessing its specific value.

Finally, I hope this article will be helpful to you, and I hope you can support the blogger. Subsequent bloggers will also regularly update the learning records to record every bit of the learning process. If there is any incorrect content in this article, please feel free to discuss it with me in the comment area!

The knowledge points of the article match the official knowledge files, and you can further learn related knowledge. Algorithm skill tree Home page Overview 57617 people are learning the system