Pointer depth advanced "six" (two-dimensional array related knowledge)

 Personal homepage: Welcome everyone --> Populus euphratica under the desert

☄The   focus of this issue: the understanding chain related to two-dimensional arrays

 I hope you all have a happy study and work every day.

 

content

Two-dimensional array

Memory layout of a two-dimensional array

3D array memory 

2D array array name

Two-dimensional array example

Dereference correlation

 next notice


Two-dimensional array

Memory layout of a two-dimensional array

First of all, in your usual study, most of the schematic diagrams of two-dimensional arrays are in the form of matrices, so is this right?

Look at an example analysis:

	char a[4][5] = { 0 };

	for (int i = 0; i < 4; i++)
	{
		for (int j = 0; j < 5; j++)
		{
			printf("%p\n", &a[i][j]);
		}
	}
    return 0;

After careful observation, I found that the memory in the two-dimensional array is continuous, which is normal. After all, when talking about the array, it is said that the array is to open up space as a whole, so it will be continuous. Then this seems to be different from the usual drawing. It should be drawn for the following look.

 This is in line with such a two-dimensional array memory layout, so that we can treat the two-dimensional array as four one-dimensional arrays, each of which is of type int [5].

3D array memory 

So what if it's a multidimensional array? (3D array)

 We can think of a three-dimensional array as four one-dimensional arrays composed of int[4][5], and int[4][5] can be regarded as four one-dimensional arrays, each of which is int[5] ]type. This is a bit like degrading one by one, so no matter how many-dimensional arrays are, they can be regarded as composed of some one-dimensional arrays.

2D array array name

There are some uses of two-dimensional array names that are a bit confusing, let's have a deep understanding of array names.

Look at the code:

    char c[3][4] = { 0 };
 	printf("%p\n", &c);
	printf("%p\n", c);
	printf("%p\n", &c[0][0]);

 Here we see that the expressions of different array names are the same value. Let's draw and analyze:

 &c is the overall address of the array, c is the address of the first element of the array (the first element type after the two-dimensional array is regarded as a one-dimensional array), &c[0][0] is the address of the first char type in the array, because for any For the target that can perform the address fetch operation, the address it takes out is the lowest address, and the lowest address happens to be the address of &c[0][0], so the number of addresses is the same in value, but the step size is The same, the root cause is because of the different types.

Two-dimensional array example

Let's look at a classic example to further understand two-dimensional arrays, pointer arrays

	int a[5][5] = { 0 };
	int(*p)[4] = a;
	printf("%p\n%d\n", &p[4][2] - &a[4][2], &p[4][2] - &a[4][2]);

The diagram below explains:

First of all, the starting addresses are the same, p is the array pointer, the type of the array is int [4], each time p+1 is to move back four integer size spaces, that is, the colored part on the picture, and the two-dimensional array int [ 5][5] can be regarded as 5 one-dimensional arrays, the type is int [5], each time +1 is to access the size of 5 integers backwards.

So p[4][2] is light brown , &p[4][2] is the place as shown in the figure, a[4][2] is dark brown, and &a[4][2] is as shown in the figure , the address value is subtracted, indicating the number of types of the first element in an array, then %p is unsigned -4, %d is signed -4, which is simple.

Dereference correlation

About two-dimensional array dereferencing.

	int a = 10;
	int *p = &a;
	int **pp = &p;

	p = 100;
	*p = 100;
	pp = 100;
	*pp = 100;
	**pp = 100;

 This is the approximate pointer map. First, p points to the lowest address of a, and pp points to the lowest address of p. p put a address

pp puts the address of p .

First p = 100 means that the pointing of p has changed.

*p = 100, because *p is the value of a, that is, changing the value in a to 100.

pp = 100, in fact, the point of pp has been changed.

*pp = 100, in fact, *pp is the content of p, which is &a, so the point of p is changed, and the effect of p = 100 is the same.

**pp = 100 actually dereferences the contents of p first, then dereferences it, dereferences p, accesses the value of a, and changes the value of a to 100, which is actually the same as *p = 100.

 next notice

In the next issue, our function pointer related content

The next issue will be more exciting~! ~!

Guess you like

Origin blog.csdn.net/m0_64770095/article/details/124258532