C language--pointer advanced 3--array pointer

Array pointer definition

analogy:

Integer pointer --pointer to integer

int a = 10;
int* pa = &a;

Character pointer --pointer to a character

char ch = 'a';
char* pc = &ch;

Array pointer--pointer to an array

int arr[10] = { 0 };
int(*p)[10] = &arr; // 数组指针

Difference : pointer array and array pointer

int(*p1)[10]; // 数组指针
int* p2[10];  // 指针数组

p1 is first combined with *, indicating that p is a pointer variable, pointing to an array of size 10 integers, all p1 is a pointer, pointing to an array, p1 is an array pointer.

p2 is an array with 10 elements, storing int* type, and p2 is an array of pointers.

Note: [ ] has higher priority than *, and () must be used to combine p1 and * first.

array name and & array name

In most cases, the array name is the address of the first element of the array, but there are two exceptions:

1 & array name, the array name represents the entire array

2 sizeof (array name), the array name represents the entire array, and the size of the entire array is calculated.

int arr[10];

For the above array, what do arr and &arr mean?

We know that arr is an array name, and the array name represents the address of the first element. Whether &arr is the same as arr, first look at the following code.

int main()
{
	int arr[10] = { 0 };
	printf("arr = %p\n", arr);
	printf("&arr= %p\n", &arr);

	return 0;
}

operation result:

It can be seen that &arr and arr print the same address, so does it represent the address of the first element of the array like arr? Look at the following code:

int main()
{
	int arr[10] = { 0 };
	printf("arr = %p\n", arr);
	printf("&arr= %p\n", &arr);
	printf("arr+1 = %p\n", arr + 1);
	printf("&arr+1= %p\n", &arr + 1);

	return 0;
}

operation result:

It can be found that although &arr and arr have the same value, their meanings should be different. In fact, &arr represents the address of the entire array . In this example, the type of &arr is int(*)[10], which is the array pointer type. &arr+1 offsets the size of the entire array by 40 (the printed address is in hexadecimal), and arr+1 offsets it by an integer size.

Therefore, although the values ​​​​of the array name and & array name are the same, their meanings are not the same. It can be understood that one is the address of the first element of the array, and the other is the first address of the array. The essence is that their types are different, and one is the same type of pointer. The other is an array pointer.

Now that you understand the concepts of array pointers and & array names, let’s take a look at what the (pointer) array pointer type looks like:

int* arr[10]; // (整型)指针数组
int* (*p)[10] = &arr; // (整型指针)数组指针

Use of array pointers

Introduction--pointer access to one-dimensional arrays

We know that arrays can be accessed through subscripts or pointers, as follows:

int main()
{
	int arr[10] = { 1,2,3,4,5,6,7,8,9,10 };
	int sz = sizeof(arr) / sizeof(arr[0]);
	// 下标访问数组
	for (int i = 0; i < sz; i++)
	{
		printf("%d ", arr[i]);
	}
	printf("\n");
	// 指针访问数组
	int* p = arr;
	for (int i = 0; i < sz; i++)
	{
		printf("%d ", *(p + i));//*(p+i)与p[i]等价
	}

	return 0;
}

operation result:

Array pointer access to two-dimensional array

    int arr[3][3] = { 1,2,3,4,5,6,7,8,9 };

Access two-dimensional arrays via subscripts:

int main()
{
	int arr[3][3] = { 1,2,3,4,5,6,7,8,9 };
	for (int i = 0; i < 3; i++)
	{
		for (int j = 0; j < 3; j++)
		{
			printf("%d ", arr[i][j]);
		}
		printf("\n");
	}

	return 0;
}

operation result:

arr[i] is actually the address of the first element of each row of the array, arr[i][j] is equivalent to *(arr[i]+j). For a two-dimensional array, its first element is the first row of the array, and its array name is the address of the first row of the array, which is an array pointer. Similar to using a pointer to access a one-dimensional array, we can define an array pointer to access a two-dimensional array, but this is not the real use of the array pointer.

One of the main uses of array pointers is to receive a two-dimensional array as a parameter, for example:

void print_arr(int(*arr)[3], int row, int col)
{
	int i = 0;
	for (i = 0; i < row; i++)
	{
		for (int j = 0; j < col; j++)
		{
			printf("%d ", arr[i][j]);
		}
		printf("\n");
	}
}
int main()
{
    int arr[3][3] = { 1,2,3,4,5,6,7,8,9 };
	print_arr(arr, 3, 3);

    return 0;
}

operation result:

Understanding two-dimensional array names:

In fact, the essence of passing two-dimensional array parameters (int arr[][3]) is to pass an array pointer, which is the array name of a two-dimensional array. Array parameter passing and pointer parameter passing will be explained in depth in the next article.

Continue to learn advanced pointer content, see the next article: Array Parameters/Pointer Parameters

Guess you like

Origin blog.csdn.net/2301_79391308/article/details/132995096