C language Learning at this time, for pointers and arrays of the relationship should have a very clear understanding of the connection between the two and the difference, but the relationship between the two is often a C language beginner easy to carry things.
There are two points worth noting in the C language:
(1) C language has only one-dimensional array, and the size of the array must be determined as a constant at compile time. The elements of a C-language array can be any type of object, such as integers, characters, and so on, but can also be another array. This allows you to "emulate" a multidimensional array.
(2) for an array, we can only do two things: Determine the size of the array, and get a pointer to the element labeled 0 in the array. Other seemingly array-like operations, even if they appear to operate on an array subscript, are essentially through pointers. In other words, any array subscript operation is equivalent to a corresponding pointer operation.
1. Array declaration
Understanding the learning array requires first understanding how to declare an array, for example,
int a[3]; Is the most basic way to declare, which means that a is an array of 3 shaping elements.
struct { int p[4]; Double x;} b[];
The above declaration represents B as an array of 17 elements, the element of which is a struct, and each body includes an array p with 4 shaping elements and a variable x of a double type.
int c[[]; This statement declares that C is an array that has 12 elements of an array type, each of which is an array of 31 shaping elements. If c is not an operand for sizeof, but is used in other situations, C is always converted to a pointer to the starting element of the calender array exponentially. To understand this, let's review some of the definitions and operations of pointers.
2, the operation of the pointer
Any pointer is pointing to a variable type, such as
int *ip; is a pointer IP that points to an integral type.
int i; Declares an integer
We can assign the address of the variable i to the pointer ip with IP = &i; , and we can assign a value to *ip to change the value of I. If you add 1 to a pointer, the pointer points to the next element, and if you subtract 1 from the pointer, the pointer points to the previous element.
Summarizing arrays and pointers, if one of our pointers points to an element in an array, then we just need to add 1 to the pointer to get a pointer to the next element in the array. Similarly, if you subtract 1 from the pointer, you get a pointer to the previous element in the array. Other plus 2, plus 3, and so on.
Let's analyze a piece of code:
int a[3]; int *= A;
In this code, p = A assigns the address of the element labeled 0 in array A to p, and must not be written as P = &a; so that p points to the element labeled 0 in array A, p+1 points to the element labeled 1 in array A, p+2 points to the element labeled 2 in array A. With this type of question, if you want to find an element in array a subscript 1, you can write P = p+1;
Another conclusion can be drawn from the above discussion that *a is the element labeled 0 in array A. For example, write *a = +; This statement sets the element labeled 0 in array A to 100. A similar * (A + 1) is a reference to the element labeled 1 in array A, and so on * (A + i) is the reference to the element labeled I in array a, simplified to a[i]. It should be clear that the relationship between pointers and arrays is discussed here.
To figure out the relationship between arrays and pointers, now let's say a two-dimensional array, declaring a two-dimensional array, declaring an integer and an integer pointer.
int c[[to]; int *p; int i;
The meaning of the C array is also mentioned before, and the C array is an array with 12 elements of array type, and each array type element is an array of 31 integers. So we can understand C[1 's meaning, c[1] is the 2nd element of the C array, which is one of 12 arrays in the C array with 31 integral elements, i.e. c[1] is an array of 31 integral elements.
The same two-dimensional array can also be written in the form of pointers, for example, the bottom three is actually the same:
i = c[4[7= * (c[474) +7);
In summary, the operation of the array is essentially about pointers, and the biggest difference between arrays and targets is that the size of the array must be deterministic, and pointers are not necessarily.
Pointers and Arrays