C - Detailed explanation of array pointers and pointer arrays

·Explanation of array pointers and pointer arrays

Array pointer

1. Distinguish

First we need to understand what is an array pointer and what is an array of pointers, as shown below:

int *p[5];
int (*p)[5];

The meaning of the array pointer is to refer to the array through the pointer, p is first combined with *, indicating that p is a pointer variable, pointing to an array of size 5. So, int (*p)[5] is an array pointer. int *p[5] is an array of size 5 that stores integer pointers.

Second, the pointer of the array element

1. Definition

Since a pointer variable can point to a variable, it can also point to an array element, so the pointer to an array element is the address of the array element.

It is written as:

int *p=arr;
int *p=&a[0];
Here we need to make it clear again that the array name does not represent the entire array, but only the address of the first element of the array, so the above two statements are the same.
2. Operation

Since the pointer points to an address, the array pointer can also perform related operations; for example, the addition and subtraction of the pointer can realize the function of the pointer pointing to the previous or next element of the array. It should be noted here that it is meaningless to perform multiplication and division in array pointers.

As shown below:


When defining a pointer variable, you need to define the type. If the pointer p points to an element in an array, then p+1 does not add 1 to the address, but adds the bytes occupied by an array element after the system determines the type. number (that is, p+1*d).

3. Reference array elements through pointers

The code is as follows:

#include<stdio.h>
intmain()
{
	int a[10]={1,2,3,4,5,6,7,8,9,0};
	int *p;
	for(p=a ; p<(a+10) ; p++)
	{
		printf("%d ",*p);
	}
	printf("\n");
	return 0;
}

First let the pointer p point to the first element of the a array, print *p (that is, the value pointing to the array), and then execute p++, so that p points to the next element, until the ten elements of the array are output.

3. Referencing multidimensional arrays through pointers

1. The address of a multidimensional array element

Let's take a two-dimensional array as an example. First of all, we need to make it clear that the address of the first element of the two-dimensional array is not a single element, but the address of the first row, as shown in the following figure:


Below we list the representation of :

representation meaning
a     Two-dimensional array name, pointing to a[0]
a[0], *(a+0), *a 0 row 0 column element address
a+1, &a[1] 1 line first address
a[1], *(a+1) address of a[1][0]
a[1]+2, *(a+1)+2, &a[1][2]

address of a[1][2]

*(a[1]+2), *(*(a+1)+2), a[1][2] the value of a[1][1]

The above figures are all different representations of addresses in a two-dimensional array.

2. Pointer variable to a multidimensional array

Outputting each value can still be done like a one-dimensional array, but here we can introduce a new method:

intmain()
{
	int a[3][4]={{1,2,3,4},{5,6,7,8},{9,10,11,12}};
	int (*p)[12];
	int i=0;
	p=&a;
	printf("%d\n",(*p)[11]);
	return 0;
}

Just like our previous example, int(*p)[12] means to define a pointer variable that points to a bunch of arrays containing 12 integer elements. We can save the entire a array by setting it to 12. Of course, we can also save 4 in a row. In the end, it is much more convenient to output.

4. Array pointer as function parameter

Here are a few ways to pass parameters:

void test()
{
 int arr[3][5] = {0};
 print(arr);
}
void print(int arr[3][5])
{}
void print(int arr[][5])
{}
void print(int **arr)
{}
void print(int (*arr)[5])
{}

We can see that the third method is obviously not feasible. A second-level pointer is referenced here, but as we mentioned above, the address of the array should be placed in the array pointer. And the fourth method is what we mentioned above, and this method is feasible.

The following is an example of a first-level pointer parameter passing:

#include <stdio.h>
void print(int *p, int sz)
{
 int i = 0;
 for(i=0; i<sz; i++)
 {
 printf("%d\n", *(p+i));
 }
}
intmain()
{
 int arr[10] = {1,2,3,4,5,6,7,8,9};
 int *p = arr;
 int sz = sizeof(arr)/sizeof(arr[0]);
 print (p, sz);
 return 0;
}

an array of pointers

An array of pointers

1. Definition

int *p[5];

The above is the example we just gave, which is the simplest array of pointers. So we can derive the definition of an array of pointers. Array of pointers: The elements of an array are all pointer type data, which is called an array of pointers.

Suppose we define an array, each element points to a different string, as shown below:


As shown in the figure above, we define a pointer array arr, and then assign the address of each string to each element, namely arr[0] and arr[1].


The output can be done with a simple function.

Second, the pointer to the pointer data

First we can define a pointer variable that points to the pointer data:

char **p;

In order to facilitate our understanding, in fact, **p is also equal to *(*p). *p indicates that p is a pointer variable, and the preceding * indicates that *p points to data of type char *. In other words, if you refer to *p, you get the value pointed to by p, which in the above example is the strings "Hello" and "World".

We can do this with the following code:

intmain()
{
	char *arr[]={"Hello","World"};
	char **p;
	int i;
	for(i=0; i<2; i++)
	{
		p=arr+i;
		printf("%s ",*p);
	}
	return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325459804&siteId=291194637