Preface
When learning C language, pointers are definitely a hurdle. Many people talk about pointer color changes and use them carefully. "All pointers are paper tigers." At the same time, we must "despise pointers strategically and attach importance to pointers tactically ".
This article first introduces the multi-dimensional situations and then the blog continues to update.
Article process:
1. differentiate pointer and array
2. Identify the time when they are the same
3. Summary
Why are pointers and arrays so ambiguous?
First of all, the usage of pointers is much more like that of arrays than they are. Therefore, in the beginning, many people want to understand them easily. However, this is true!
Here is an example:
Array [the above is the definition in file 1
* Array
The above is the declaration in file 2
Is the above example correct? You can test it on your own and find it amazing: compile, how to compile it! What about it?
Further in-depth descriptions and definitions
Before analyzing arrays and pointers, let's talk about the differences between declarations and definitions. In the previous blog post, "why are you not proficient in C? 03 this point has been mentioned at the end of the in-depth analysis statement. Let's repeat it here:
In C, objects can be declared.
Definition: it can only appear in one place and is determined to allocate memory at the same time. It is a special declaration.
Declaration: only objects created elsewhere. Has an extern prefix, acting on the variable
For, just like assertion, because the Declaration also specifies the moving method and length of the pointer/array.
So, for the above example, we can know that in file2.c, array is declared as an int pointer. However, the definition in file1.c is an array. They are not compatible! What about it?
Data access through pointers and arrays is different
Here we first add a knowledge point, the sizeof (x) operator, which returns the number of bytes in the memory of x.
#include array[ printf( printf(,,(), (* printf(,,(), (* printf(,,(), (* printf(,,(), (* printf(,,(), (* printf(,,(), (* }
Here, we can conclude that all are 4 bytes (because my computer is 32-bit = 4*8 bit. Similarly, 8 is displayed on a 64-bit computer ).
Let's take a closer look at sizeof (array), sizeof (& array [0]), (because we all say that the array name is the first address pointer, so I said this .)
It can be seen that 80 characters are printed, that is, the length of 20*4 and 20 int characters. But still 4, the fixed length of the pointer. That's the big difference!
Well, through sizeof, we can realize that there are actually differences between the original array and pointer. Now let's continue.
As we can know from sizeof, when defining an array, it is equivalent to the continuous allocation of N-length space in the memory. The first address of the array (assuming 5000) represents the starting point of the space. For example, when accessing through array [I], the steps are as follows:
- Move to address 5000 + I * 4
- Take the value that exists here
What about passing a pointer? We only store the address value in the pointer, for example, int * p = array. Here, assume that the address of p is 6000, And the saved value is the first address of array 5000, access through p [I] as follows:
- Take the value 5000 in Pointer p and move it to this address.
- Move to address 5000 + I * 4
- Take the value that exists here
We can see that there is one more step. As we all know, this shows the flexibility of pointers.
Initialization of string arrays and pointers
We can initialize the character array as follows:
*p = a[] = ;
Let's take a look at the implicit differences:
For pointer p, the system assigns us an anonymous one. This constant is, and its address is given to p, so the value of the String constant.
For array a, the system defines a continuous memory block to allocate a string. Its first address is a, and we operate to modify the value of a character.
String Array, pointer
In C language, the first address of an array is defined as "unchangeable left value", that is, a declaration similar to int * const a. We can use it to modify the content it points, but it cannot be assigned a new value. That is, a [I] = yyy is allowed, but a = xxx is not prompted!
Pointer, let's see how it is declared. Generally, it is int * p. We can p = a, p = xxx, p [I] = yyy ......, Are allowed.
Well, the general difference is the above expression. Let's continue to discuss when they will be the same.
Pointers are equivalent to arrays.
We know that pointers are full of flexibility, while arrays are relatively rigid. In this case, we must first understand arrays to better understand pointers. let's first talk about arrays. For the C language, everything must be declared and used again. In this order, let's first declare:
Array declaration
- 1. External array declaration
- 2. array Definition
- 3,
Use of Arrays
The array name in is a pointer, and you can use; and other forms to express the same meaning.
2. The C language uses the array subscript as the pointer offset
3. That is:
my_func(* p[]);
All mean, And the compiler translates it into pointers.
Summary
Using the summary in "C expert programming", I made some modifications and merged some. The author suggests that, after we really understand arrays and pointers, we should first summarize ourselves and then look at his summary.
1. the compiler interpreted the expressions in the form of a [I] As * (a + I );
2. Pointers are always pointers and cannot be rewritten into arrays. You can access the pointer by subscript, and you must know that the pointer points to an array. After all, the compiler does not know what int * points to. An int array is also an int variable address.
3. When used as a function parameter, the array is rewritten as a pointer, which is similar to p = & a [0]. Therefore, for the compiler, there is no array in the parameter table, only pointer.
4. The declaration and definition must match! If an array is defined, it must be an array when other files are declared, and the pointer is also true.