C language array pointer shift, C language array pointer_3

Array pointers and pointer arrays in C language:

Array pointer

1. Distinguish

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

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:

b14c61bf0373929c8a7c9d360ff603c4.png

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

intmain()

{

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

int *p;

for(p=a ; p

{

printf("%d ",*p);

}

printf("

");

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:

427516ae924279dc0dd637af055e6c28.png

Below we list the representation of the relevant address:

dfcc5b40672b8db75d8b13c8f4f303c8.png

representation meaning

a Two-dimensional array name, pointing to a[0]

a[0], *(a+0), *a0行0列元素地址

a+1, &a[1]1行首地址

a[1], *(a+1)a[1][0]的地址

a[1]+2, *(a+1)+2, &a[1][2]

a[1][2]的地址

*(a[1]+2), *(*(a+1)+2), a[1][2]a[1][2]的值

上图都是二维数组中地址的不同表示形式。

2.指向多维数组的指针变量

输出每一个值依然可以像一维数组一样,但这里我们可以介绍一种新的方法:

int main()

{

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

",(*p)[11]);

return 0;

}

就像我们前面的例子一样,int(*p)[12]表示定义一个指针变量,它指向一个包含12个整型元素的一堆数组。我们将其设定为12便可以存下整个a数组,当然也可以存一行4个,最后如果输出的话就方便了许多。

四、数组指针作函数参数

下面是几种传参的方式:

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])

{}

我们可以看出第三种方式明显是不行的,这边引用了一个二级指针,但是我们上面提到过数组的地址应该放到数组指针中去。而第四种方法就是我们上面提到过的,而这种方式是可行的。

下面是一个一级指针传参的例子:

#include

void print(int *p, int sz)

{

int i = 0;

for(i=0; i

{

printf("%d

", *(p+i));

}

}

int main()

{

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:

1f503377299187f154b7d4f9c0c01cf7.png

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;

}

Reference blog post:

Detailed explanation of pointer array and array pointer:

Detailed explanation of C language pointers (classic, very detailed)

Guess you like

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