[C language]--A guide to understanding pointer arrays and array pointers

Table of contents

1. What are pointer arrays and array pointers

1. Array of pointers: As the name suggests, an array of pointers is stored.

Supplement (1): Pointer arrays can also be used in combination with string arrays

Supplement (2): The difference between a two-dimensional array and an array of pointers

2. Array pointer: As the name suggests, it refers to a pointer to an array

Differences between pointers to one-dimensional arrays and pointers to two-dimensional arrays

3. Summary

4. Finally, let's compare and recall through pictures to understand.

It is not easy to create, if this blog is helpful to you, please remember to leave a message + like it.


1. What are pointer arrays and array pointers

1. Array of pointers: As the name suggests, an array of pointers is stored. It means that an array contains pointers, that is, an array of pointers is an array, and each element in the array is a pointer.


Definition form: int *a[10].

Explanation: The priority of [] is higher than *, and the definition form can be understood as: int * (a[10]); inside the brackets, a is an array containing 10 elements, and outside the brackets, the type of each element is int *.

as the picture shows:

              

Here is a simple example

#include <stdio.h>
int main()
{
    //定义三个整型数组
	int a[5] = { 1,2,3,4,5 };
	int b[5] = { 6,4,8,3,1 };
	int c[5] = { 2,5,8,6,1 };
    //定义一个存放指向整型变量的指针的数组arr
    int* arr[] = { a,b,c };
    //通过接引用打印出三个一维数组的元素
	for (int i = 0; i < 3; i++)
	{
		for (int j = 0; j < 5; j++)
        {
		    printf("%d ", *(arr[i]+j));
	    }
        printf("\n");
    }
	return 0;
}

The result is as follows:

      1 2 3 4 5

      6 4 8 3 1

      2 5 8 6 1

There are many ways to dereference arr above, and they are all equivalent. Let's give an example:

#include<stdio.h>
int main()
{
	int i = 0;
	int a[3][4] = { {1,2,3,4} ,{5,6,7,8} ,{9,10,11,12} };//定义一个二维数组
	int* pa[3];//定义一个指针数组
	for (i = 0; i < 3; i++)//给指针数组赋值
		pa[i] = a[i];
    printf("指针数组的内容为:\n");
	for (i = 0; i < 3; i++)//打印出指针数组的内容
	{
		int j;
		for (j = 0; j < 4; j++)
			printf("%d ", *(*(pa + i) + j));
		printf("\n");
	}
    //以下均为不同方式的解引用操作
    printf("不同解引用操作的结果为:\n");
	printf("%d,%d\n", a[1][1], *(pa[1] + 1));
	printf("%d,%d\n", a[1][1], *(*(pa+1) + 1));
	printf("%d,%d\n", a[1][1], (*(pa + 1))[1]);
	printf("%d,%d\n", a[1][1], pa[1][1]);
    return 0;
}

The result looks like this: 

The contents of the pointer array are:

1 2 3 4
5 6 7 8
9 10 11 12

The results of the different dereferencing operations are:

6,6
6,6
6,6
6,6

It can be seen from the above examples that there are many ways to dereference, and their equivalent forms are as follows:

*( pa[i] + j ) //equivalent to *( a[i] + j )

*( *(p+i) + j ) // 等了于 *( *(a+j) + j )

( *(p+i) )[ j ] // 等了于( *(a+i) )[ j ]

p[ i ][ j ] // 等了于 a[i][j]

Supplement (1) : The pointer array can also be used in combination with the string array , please see the following example:

#include <stdio.h>
int main(){
    char *str[3] = {"lirendada","C语言","C Language"};
 
    printf("%s\n%s\n%s\n", str[0], str[1], str[2]);
    return 0;
}

The result is as follows:

dating

c language

C Language

It should be noted that the first address of the string is stored in the character array str, not the string itself. The string itself is located in other memory areas and is separate from the character array.

Only when the type of each element in the pointer array is char *, can the pointer array be assigned a value as above, and other types cannot.
 

Supplement (2): The difference between a two-dimensional array and an array of pointers

char *p1[]={"lirendada","C","C++"};
char p2[][8]={"liren","C","C++"};

*p1, *(p1+1), *(p1+2): the pointed string constant is of irregular length, and sizeof(p1)=12.


The strings pointed to by p2[0], p2[1] and p2[2] are all of a certain length, and sizeof(p2)=24.

2. Array pointer: As the name suggests, it refers to a pointer to an array, which is essentially a pointer, but only points to an array.

Definition form: int (*p)[10]; Among them, since [] has a higher priority than *, (*p) must be added.

Explanation: The * in the brackets indicates that p is a pointer, and the pointer variable stores the first address of an array , which points to an entire array (), and the type of the array is int[10], which is exactly the type of each one-dimensional array contained in a.

Under the 32-bit system, the memory size occupied by any type of pointer is 4 bytes . As for how many bytes it points to, the array depends on the size of the array. In short, the array pointer is "pointing to the size of the array".

as the picture shows:

     

 

Take a chestnut:

    int arr[10]={1,2,3,4,5,6,7,8,9,10};
    int (*p)[10]=&arr;//数组的地址要存起来
    //上面的p就是数组指针
    int i=0;
    for(i=0;i<10;i++)
    {
        printf("%d ",*(*p+i));//第一种方法打印数组
    }
    printf("\n");
    for(i=0;i<10;i++)
    {
        printf("%d ",(*p)[i]);//第二种方法打印数组
    }
    printf("\n");

Array pointers are generally used in two-dimensional arrays, for example:

//参数是指针的形式
void print(int(*p)[5],int x,int y)
{
    int i=0;
    for(i=0;i<x;i++)
    {
        int j=0;
        for(j=0;j<y;j++)
        {
            printf("%d ",*(*(p+i)+j));//*(p+i)=p[i],所以*(*(p+i)+j)=p[i][j]
        }
        printf("\n");
    }
}

int a[3][5]={
   
   {1,2,3,4,5},{2,3,4,5,6},{3,4,5,6,7}};
print(a,3,5);
//1 2 3 4 5
//2 3 4 5 6
//3 4 5 6 7
//此处将每一行看成一个元素,利用数组指针将二维数组看成一维数组int [5]

Differences between pointers to one-dimensional arrays and pointers to two-dimensional arrays

1. A pointer variable pointing to a one-dimensional array Let the one-dimensional array be a[n]

Definition method: * The pointer variable name is * P,
the first address of the one-dimensional array that p generally points to, that is, p=a, or p=&a[0]; p, a, &a[0] all point to the same unit, which are the first address of the array a and the first address of the 0th element a[0]. p+1, a+1, &a[1] all point to element a[1]. By analogy, we can know p+i, a+i, & a[i].

2. A pointer variable pointing to a two-dimensional array Let the two-dimensional array be a[m][n]

Definition method: (*pointer variable name)[length] (*P)[n]
"length" indicates the length of the one-dimensional array when the two-dimensional array is decomposed into multiple one-dimensional arrays, that is, the number of columns of the two-dimensional array.

Regarding arrays of pointers, we roughly follow the following rules:

(1) To access array elements, either use the subscript method [], or use the pointer *.

(2) A one-dimensional array uses one subscript, and a two-dimensional array uses two subscripts

(3) When the pointer points to an array element, the pointer indicates the address of the array element, and the value of the address (that is, the array element) is calculated by using * to operate on this address

(4) When the pointer array points to a two-dimensional array, because it itself is a pointer to the array, each addition of one to its inner layer is equivalent to the next array
 

3. Summary: It mainly depends on what the following two words are (the front is the modification function), so the pointer array is an array, and the array pointer is a pointer. The essential difference between an array pointer and an array of pointers is the priority relationship. We need to pay attention to whether the priority of the pointer or the priority of the array is high. If the priority of the array is high, it is an array of pointers. If the priority of the pointer is high, it is an array pointer.

4. Finally, let's compare and recall through pictures to understand.

1. Array of pointers

 2. Array pointer

It is not easy to create, if this blog is helpful to you, please remember to leave a message + like it.

Guess you like

Origin blog.csdn.net/m0_73381672/article/details/131317181