One-dimensional arrays and pointers relationship:
1. The one-dimensional array name simplify operation
. 1 #include <stdio.h> 2 void main () . 3 { . 4 int I, A [ . 5 ], B [ . 5 ]; . 5 int * P; . 6 for (I = 0 ; I < . 5 ; I ++ ) . 7 Scanf ( " % D% D " , a + i, & b [i]); // a i + directly at the address, and remove the standard & compared to the address b i of the array, the two equivalent, but The former method is no longer an ampersand.
Then there is a problem: that a [i] = i [a] it?
According to C99, the compiler uses the pointer arithmetic internally to access array elements. For example: a [8] = * (a + 8), then in accordance with exchangeable addition, * (a + 8) = * (8 + a) = 8 [a]
2. The one-dimensional array of numbers pointer operation
Can be done by any array subscripting, pointers can borrow be implemented by means of the pointer from the Operational simplify operation, using a pointer to an array storage helps produce occupies a small space, fast, high-quality code , but pay attention to cross-border issues pointer!
// then the above code P = A; for (I = 0 ; I < . 5 ; I ++ ) the printf ( " % D " , * (P + I)); // * (P + I) == P [I ] == * (a + I) == a [I] // converted address the while (P <+ a . 5 ) { the printf ( " % D " , * P); P ++ ; } // At this point p = a + 5, for normal operation in order p, p needs to redirect a known address! = A & P [ 2 ]; // where P [0] = A [2]; for (I = 0 ; I < . 3 ; I ++ ) the printf ( " % D% D " , P [ 2 + I], P [ 2 -i]); // are a [2], a [2 ], a [3], a [1], a [ 4], a [0]
3. Using a one-dimensional array of characters
A one-dimensional array of characters is a string!
1 #include<stdio.h> 2 void main() 3 { 4 int a[[]={1,2,3,4,5},*p,i; 5 char c[]="abcde",*cp; 6 p=&a[2]; 7 cp=&c[2]; 8 for(i=0;i<3;++i) 9 printf("%d%c%d%c",*(p+i),*(cp+i),*(p-i),*(cp-i)); 10 priintf("\n%d%s%c\n",*p,*cp,cp);
Compile error-free, but a run-error (Code tenth row), * cp represent a character you want to use% c, and cp is the first address of the memory strings, so the output from the address pointed to by cp beginning of the string, you need to use % s format, it will be changed
printf("\n%d%c%s\n",*p,*cp,cp);
Please note that the issue of the string end position '\ 0'