Example
The code is as follows: |
Copy code |
# Include <stdio. h> # Define SIZE 4 Int main (void) { Short dates [SIZE]; Short * pti; Short index; Double bills [SIZE]; Double * ptf; Pti = dates; Ptf = bills; Printf ("% 23 s % 10sn", "short", "double "); For (index = 0; index <SIZE; index ++) Printf ("pointers + % d: % 10 p % 10pn", index, pti + index, ptf + index ); Return 0; } |
First:
In C, the result of adding 1 to a pointer is to add a storage unit to the pointer. For the mouse family, the address is added to the address of the next element, rather than the next byte.
Therefore, we can see that his address is output:
The code is as follows: |
Copy code |
Short double Pointers + 0: 0028FF0C 0028FEE8 Pointers + 1: 0028FF0E 0028FEF0 Pointers + 2: 0028FF10 0028FEF8 Pointers + 3: 0028FF12 0028FF00 |
The reason for the difference is that the number of bytes occupied by short and double is different. (Short uses two bytes, and double is eight)
* (Dates + 2) is different from * dates + 2. Because * has a higher priority than *, it will take precedence. This is equivalent to adding 2 to the value Currently pointed to by dates.
Let's take a look at another one:
The code is as follows: |
Copy code |
# Include <stdio. h> # Define MONTHS 12 Int main (void) { Int days [MONTHS] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31 }; Int index; For (index = 0; index <MONTHS; index ++) Printf ("Month % 2d has % d days. n", index + 1, * (days + index )); Return 0; } |
We can see that * (days + index) is actually effective with days [index.
In the function, we can input the int sum (int * ar) array, but only the address of the first element can be obtained, but we do not know the number of arrays (we can naturally pass in the number of arrays as the second parameter), it can also be replaced by int ar [], which is a pointer to the array, it is a pointer (because it is important to say it twice). Why do you emphasize this? You can see this program:
The code is as follows: |
Copy code |
# Include <stdio. h> # Define SIZE 10 Int sum (int ar [], int n ); Int main (void) { Int marbles [SIZE] = {20, 10, 5, 39, 4, 16, 19, 26, 31, 20 }; Long answer; Answer = sum (marbles, SIZE ); Printf ("The total number of marbles is % ld. n", answer ); Printf ("The size of marbles is % lu bytes. n", sizeof marbles ); Return 0; } Int sum (int ar [], int n) { Int I; Int total = 0; For (I = 0; I <n; I ++) Total + = ar [I]; Printf ("Te size of ar is % lu bytes. n", sizeof ar ); Return total; } Let's see the output: Te size of ar is 4 bytes. The total number of marbles is 190. The size of marbles is 40 bytes. |
When calling a function, the output is 4, because in the function, it is not an array, but an int pointer. The int type is 4 bytes, while in main, the array itself consists of 10 int types, so it is 40.
Remember at any time: * (days + index) is actually effective with days [index].
In addition to specifying the array size, how can I specify it:
The code is as follows: |
Copy code |
# Include <stdio. h> # Define SIZE 10 Int sump (int * start, int * end ); Int main (void) { Int marbles [SIZE] = {20, 10, 5, 39, 4, 16, 19, 26, 31, 20 }; Long answer; Answer = sump (marbles, marbles + SIZE ); Printf ("The total number of marbles is % ld. n", answer ); Return 0; } Int sump (int * start, int * end) { Int total = 0; While (start <end) { Total + = * start; Start ++; } Return total; } |
The answer is the same. Because the index starts from 0, marbles + SIZE points to the next element after the end of the array (the result is unknown ).