Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and beauty - Computer two-level C language problem solving
Computer two-level C language problem solving
Firstly, the concept that an array is a pointer and a pointer is an array is given. Look down first if you don't understand, and then I'll explain.

( 1) swap 1(a,a+ 1);

The two parameters of Voidswap 1 (intc0 [], intc 1 []) function are two arrays with indefinite length, which can also be regarded as two pointers.

Looking at the program body again, we can see that swap 1 has completed the exchange of values c0[0] and c 1[0] [] pointed by two pointers.

And a, a+ 1 are two addresses (that is, pointers), pointing to a [0] and a [1] respectively (the array name a is defined as a pointer to the first element of the array).

When swap 1(a, a+ 1) is executed, the values of a, a+ 1 (that is, the addresses of a [0] and a [1]) are copied into two pointers, namely C0 [] and c/kloc-. At this time, C0. "swap 1" has completed the exchange of values pointed by C0 [] and C 1 [], that is, the exchange of A [0] and A [1].

So array a becomes 5, 3.

(2)swap 2(&; b[0],& ampb[ 1]);

The two parameters of void swap2(int *c0, int *c 1) function are obviously two pointers.

Looking at the program body again, we can see that swap2 has completed the exchange of values * C0 and * C 1 pointed by two pointers.

And & b[0], & ampB [1] are two addresses (that is, pointers), pointing to B [0] and B[ 1] respectively.

Perform Exchange 2 (&; b[0],& ampB[ 1])、&; B[0], & Copy the values of b [1] (that is, the addresses of b [0] and b[ 1]) to two pointers, namely C0 and C 1. At this time, the two pointers of C0 and C 1 point to B [0] and B [1] respectively. "swap2 has completed the exchange of values * C0 and * C 1 pointed by two pointers", that is, the exchange of B [0] and B [1].

So array b becomes 5, 3.

(3) The final result:

The Printf statement outputs a [0], a [1], b [0] and b [1] in turn, and the result is 5353.

(4) Back to the beginning

Learning pointers doesn't understand the relationship between pointers and arrays, which will be very confusing in the future, especially multi-bit arrays.

Arrays are pointers:

First, C program sees Int A [10]; It will open up a section of memory, which can continuously put down the plastic data of 10.

When assigning a value to an array, for example, a [3] = 0; . C program recognizes it as "*(a+3)=0", that is, the value pointed by a+3 address is assigned to 0.

(pay attention to the following points! At this time, the continuous storage of elements in the array plays an important role. At this time, the C program automatically creates a new pointer, which automatically points to the first address of this memory, that is, to A [0]; At the same time, specify that the pointer points to the same plastic data as the elements stored in the array.

Therefore, the pointer moved backward from pointing to a[0] by 3 plastic data units, that is, the total * * * moved by 6 bytes to point to a[3]. At this point, the pointed a[3] is assigned to 0, and then the temporary pointer disappears automatically.

Printf("%s ",arr) when outputting a string (a string is an array whose elements are characters with an automatically added null operator at the end); Do it. C program outputs each element of the string in turn from the first element of the string arr, and arr [0] is the element pointed by the automatically defined pointer with the value of arr until it encounters the null operator.

Generally, arrays don't have the last empty operator, so they can't be output as a whole and can only be output one by one. Each output will create a new temporary pointer, point to the element to be output, complete the output, and close the temporary pointer.

Pointers are arrays:

Defining a pointer p is actually to find a location in memory and assign its address to a pointer. If you copy to the following memory unit immediately at this time, such as pointer P or p+5, it may change the important data in the memory. Therefore, people usually immediately let the newly created pointer point to the newly defined array. This is different from defining an array separately because it uses a special pointer instead of a temporary pointer.

The advantage is that it won't disappear, and you don't have to move from the array every time. For example, if you output ARR [1] and ARR [2], you will create a temporary pointer every time, move it to the specified position, output it and close it. Pointer P can output arr[ 1] after positioning to arr[ 1] once, and then output arr[2] immediately after moving back one bit, thus saving the time for creating new pointers and positioning.

The disadvantage is that people must always pay attention to the direction of the pointer, and if they are not careful, they may assign wrong values or output errors, which is also the reason why novices often make mistakes when they encounter pointers.

Needless to say, with the above description, think about the relationship between the pointer of the pointer and the pointer of the array (used when using multidimensional arrays).

Finally, a pointer is the same as an array. The root cause is that the elements of the array are stored continuously, which just meets the most common operation of pointers-moving and then processing values. The combination of the two can greatly improve the running speed of the program, and the programmer will not be careless or make mistakes.