Pointer form and subscript form
a> char *p = "abcdef";//defines a string that points to the string with a pointer
B>char a[] = "123456";//define a character array A is the first address of the first element of the array, not the first address of the array
In the a> definition above, if we want to remove the C character, then there are two ways:
<1> the form of the pointer: * (P+3)
<2> subscript form: p[3]
There are two ways that we can understand this:
The compiler first calculates The value of P, which is the address value stored by P, and then an offset of 3, to get a new address,
And then take out the data stored in the new address, which is the character C.
For example, you can write out the following program to output a string defined in a>.
#include <stdio.h>int main (void) { char *p= "abcdef";//a> int ioffset = 0; For (Ioffset=0;ioffset<strlen (p); ioffset++) printf ("%c", * (P+ioffset)); return 0;}
Similarly, in the b> definition, if we want to take out 5 of this data, there are two ways:
<1> the form of the pointer: * (A+4); At this point, a represents the first address of the first element of the array, and then uses this address to add an offset,
Get a new address and fetch the data from this address
<2> the form of subscript: a[4];
It is worth noting that, without knowing what you have found, there is a problem with the above mentioned offset, after removing the base address (P-value in a> in,b>, a addresses),
The offset should be the number of offsets x the size of each element. In the example above, the memory size of the character type is exactly 1, so it is easy to mislead. Special attention!
C Language Study-form of the pointer & subscript