Pointer depth analysis "four" (the "intimate" relationship between pointers and arrays)

Today we mainly talk about the intimate relationship between pointers and arrays. What are the similarities and differences between them?

 

Have a good time everyone today!

 

 

Are pointers and arrays related?

Everyone knows that arrays and pointers are similar in terms of access, so is there a relationship between them? Let me tell you.

Define a string as a pointer and access it

First, we define a string with a pointer and access it in two ways (pointer access, and [] access)

	const char *p = "holle boy";
	int len = strlen(p);

	printf("指针形式访问\n");
    for (int i = 0; i < len; i++)
	{
		printf("%c ", *(p + i));
	}

	printf("\n数组形式访问\n");
	for (int i = 0; i < len; i++)
	{
		printf("%c ", p[i]);
	}

 

It can be seen that p can use any access method, so does this mean that pointers are related to arrays? (not sure for now)

In addition, let's say p, this opened string is in the character constant area in memory, even if there is no const, the internal value cannot be modified, because this is the protection at the operating system level. The address of the pointer variable p is stored in the stack frame of the main function, and the point of p is the first address of the string created in the character constant area, a sketch for your reference.

 

Define a string as an array and access it

Then define a string in the form of an array, and access him.

    char q []= "holle girl";
	int len = strlen(q);

	printf("指针形式访问\n");
	for (int i = 0; i < len; i++)
	{
		printf("%c ", *(q + i));
	}

	printf("\n数组形式访问\n");
	for (int i = 0; i < len; i++)
	{
		printf("%c ", q[i]);
	}

 This is created by the array and accessed in two forms. It seems that arrays can also be accessed in the form of pointers, so what is the relationship between pointers and arrays? (still not sure)

Compare the difference between the two

Let’s talk about a small example first. If there is your twin in this world, its body shape and appearance are the same as you, and even the work and rest are the same as you. Is your brother exactly the same as you?

First of all, you may have many similarities, even the same work and rest, but in essence, you are two completely different personalities, so they are completely unequal.

Pointers and arrays are like twins, they are similar, but we cannot confuse them conceptually or essentially.

Take the example above to analyze:

This is the storage of the array, which is the space opened up in the stack area. All the elements of the string are on the stack, and the value can be modified.

Both can be accessed in the form of pointers and arrays.

 

 

 This is the space opened up for storage in the form of a pointer. The address of p is stored in the stack area, and the content of the string stored in the character constant area is stored. And the value cannot be modified.

In this way, from the perspective of string storage , pointers and arrays have nothing to do with each other, but they have many similarities in terms of access methods .

Conclusion: pointers and arrays have nothing to do 

The dimensionality reduction problem of array parameters

First of all, we need to know that when the array is passed through a function call, its dimension will be reduced, and it will be reduced to a pointer.

See an example:

void Show(int arr[] /*int* arr*/, int num)
{
	for (int i = 0; i < num; i++)
	{
		printf("%d ", arr[i]);
	}
	printf("\n");
}

int main()
{
	int a[5] = { 1, 2, 3, 4, 5 };
	int num = sizeof(a) / sizeof(a[0]);

	Show(a, num);

	return 0;
}

First of all, this is to print the array elements on the screen, then you have noticed that there is no function parameter int arr[] and int *arr can print the result . why? And why can the pointer of int * receive it?

The result can be seen by debugging:

 It can be seen that the type of arr is int*, which means that arr passes the address of the first element , and the pointer of int* is used to receive without warning.

This is called dimensionality reduction of array parameters.

So why dimensionality reduction?

To put it simply, it is for the efficiency of function calls to reduce the dimension into pointers , instead of copying the array, only copy the pointer (4 bytes)

So what is dimensionality reduction?

Simply put, it is dimensionality reduction into a pointer to the internal element type . (Subsequent explanation of complex pointers will be discussed in detail)

Why should pointers and arrays be accessed the same way.

As mentioned above, array dimension reduction is all reduced to pointers, so if the access methods of pointers and arrays are different, the following situation will occur.

void Show(int arr[], int num)
{
	printf("指针形式 ");
	for (int i = 0; i < num; i++)
	{
		printf("%d ", *(arr+i));
	}
	printf("\n");
}

int main()
{
	int a[5] = { 1, 2, 3, 4, 5 };
	int num = sizeof(a) / sizeof(a[0]);

	Show(a, num);

	printf("数组形式打印");
	for (int i = 0; i < num; i++)
	{
		printf("%d ", a[i]);
	}
	printf("\n");
	Show(a, num);

	return 0;
}

This will lead to the use of pointer form every time you use an array to pass parameters, and array form to access the array, which is very troublesome. If this function needs to be executed multiple times and the array needs to be accessed multiple times, it will If there is an error, it is obviously not suitable for the usage scenario, so in the final design, the usage of arrays and pointers will be common.

Summary of this issue:

First of all, we talked about the difference in storage between pointers and arrays. It has nothing to do with deeply distinguishing pointers and arrays .

Then we talked about the dimensionality reduction of arrays, in order to pave the way for why the access methods of arrays and pointers are the same.

Then we said, because if the access methods of pointers and arrays are different, it will lead to inconvenience for users in some special cases and increase the probability of errors, so they are accessed in the same way.

Next notice:

The next issue will explain the knowledge of complex pointers.

The next issue will be more exciting~! ~! ~!

Guess you like

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