"C language depth analysis" Chapter 4 pointer and array p3 C language from introduction to soil (Advanced)

catalogue

1. Relationship between pointer and array

1.1 let's start with the conclusion:

1.2. Accessed as a pointer and as an array

1.3 their interviews are so similar. Is that a thing?

1.4 why is C designed like this?  

1.5. The difference between a and & A

2. Comparison between pointer and array (original text in the book) (just look)

This chapter is the essence of the author's summary of various materials such as "C depth analysis" and so on. The basic part is omitted a lot, so that we can have a deeper understanding of the charm of C language. Because in order to avoid repetition with previous articles, let's talk about the essence of the author. Now the text begins!

No one can stop you from becoming a better person.  

1. Relationship between pointer and array

1.1 let's start with the conclusion:

1. It doesn't matter
2. There are great similarities in element access

1.2. Accessed as a pointer and as an array

#include <stdio.h>
#include <string.h>
#include <windows.h>
#define N 10
int main()
{
        char *str = "abcdef"; //str pointer variables are saved on the stack, and "abcdef" is in the character constant area and cannot be modified
        char arr[] = "abcdef"; //The entire array is saved on the stack and can be modified

        1. Accessing pointers as pointers and Subscripts
        printf("Accessing pointers as pointers and Subscripts\n");
        int len = strlen(str);
        for (int i = 0; i < len; i++){
        printf("%c\t", *(str + i));
        printf("%c \n", str[i]);
        }

        printf("\n");
        //2. Access the array in the form of pointer and the array in the form of subscript
        printf("Accessing arrays as pointers and Subscripts\n");
        len = strlen(arr);
        for (int i = 0; i < len; i++){
        printf("%c\t", *(arr + i));
        printf("%c \n", arr[i]);
        }
        printf("\n");
        system("pause");
return 0;
} 
Conclusion: when pointers and arrays point to or represent a space, the access methods can be interconnected and similar. However, similarity does not mean that it is a thing or relevant.

1.3 their interviews are so similar. Is that a thing?

So everyone writes the same, but the addressing scheme is completely different! So pointers and arrays are not the same thing.

1.4 why is C designed like this?  

Let's talk about preparation first:

2. Why dimensionality reduction?

If the dimension is not reduced, the array copy will occur, and the efficiency of function call will be reduced, so the dimension is reduced to a pointer.  

PS: although it is reduced to a pointer, it still needs to be copied when passing parameters, but the array needs to be copied before, Now we only need to copy the pointer (address) (4 bytes or 8 bytes). Take the address of our first element as the initial value, and the initially passed in pointer variable (if int arr [] is written in this way, the number in [] will be ignored when passing the parameter, which is the same as the pointer passed later. It is not important to write (but it must be a positive number) and it is generally not written because it is ignored) (int * ARR), in other words, this arr is a variable pointing to the target space of the array

Supplementary conclusion: in c, any function call, as long as there is formal parameter instantiation, must form a temporary copy!!!

Simply put, if you write a function and pass parameters to it, there will be a temporary copy, no matter what you pass.   

3. What is dimensionality reduction?

All arrays and parameters will be reduced to pointers and pointers to their internal element types. (including high-dimensional arrays)

Come back and answer the question

In fact, there is no problem with the above code. However, I don't know whether you have found it. If the pointer and array elements are not accessed, if there are a large number of function calls and a large number of array parameters in C (procedure oriented), programmers will be required to change various access habits. As long as it is to be done by others, it is difficult to improve the probability of code errors and debugging.
Therefore, simply, C connects the access mode of pointer and array, so that programmers can access elements in the function as if using array, and the essential value reduces the difficulty of programming!

1.5. The difference between a and & A

#include <stdio.h>
#include <string.h>
#include <windows.h>
int main()
{
        int a[5] = { 1, 2, 3, 4, 5 };
        int *ptr = (int*)(&a + 1);
        printf("%d %d\n", *(a + 1), *(ptr - 1));
        system("pause");
        return 0;
}

Conclusion: & A is called the address of the array, and the right value of a is called the address of the first element of the array. The essence is that the types are different, and the + - calculation steps are different

2. Comparison between pointer and array (original text in the book) (just look)

 

 

That's all for today!!!

The next issue starts with pointer arrays and array pointers!

If you think the author is a little helpful to you!

Just a little praise and attention!!! Of course, subscription is even more desirable!

Finally, thank you for watching!!!

Your support is the greatest driving force for the author to write!!!

See you next time!!!

Keywords: C Back-end

Added by livvylove on Thu, 23 Dec 2021 07:06:41 +0200