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.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; }
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
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!!!