Pointer and array, pointer Array
The previous things are relatively basic, and the latter is the real key.
Sample Code:
# Include <iostream> # include <stdlib. h> int main (int argc, char * argv []) {// argc indicates the number of parameters. // argv [0] indicates the program name. // argv [1] indicates the first parameter int array [10] = {1023,2, 3, 4, 5, 6}; int * p = & array [0]; int * p1 = array; printf ("% p, % p \ n", array, & array [0]); // The 16-bit address // The value of the two addresses, and the two addresses are the same. Printf ("* p = % d \ n", * p); // 1 printf ("* p1 = % d \ n", * p1 ); // 1 printf ("sizeof (array) = % d, sizeof (p1) = % d \ n", sizeof (array), sizeof (p1 )); // 40, 8 printf ("% d, % d \ n", sizeof (int *), sizeof (char *); // 8 printf ("array = % p, * array = % p, & array = % p \ n ", array, * array, & array); // The address value, which is the same as the preceding result, 15 after 0, 1 (if 32-bit operating system, it should be 7 after 0, 1), address value, same as above // What is the result? Printf ("% d \ n", * (char *) p1 + 1); // This is a strange place and always feels problematic. // But it seems to be 0. Return 0 ;}
Running result:
This place is somewhat different, just to try out that result. So we can see that the first number in the starting array is 1.
Let's see why the result of the last line is 3. The previous students who do not know why can look at the content written in this address:
Http://www.cnblogs.com/letben/p/5213967.html
Void function2 (void) {// if you have understood the above, the following are simple char buf [10] = {"abcdef"}; char * pc = buf; printf ("% c \ n", * (pc + 3); int I = 0x12345678; char * cpi = (char *) & I; printf ("% x, % x \ n", * (cpi + 0), * (cpi + 1), * (cpi + 2 ), * (cpi + 3 ));}
Running result:
This shows the relevance of the operating system. If it is a common windows or linux, the first small-end output is used. If it is a server-type unix, the results of, will appear.
Assign values using pointers:
Code:
/** Assign values using pointers. */Void function3 (void) {int array [10] = {0}; int * p = array; for (int I = 0; I <10; I ++) {scanf ("% d", p ++) ;}for (int I = 0; I <10; I ++) {printf ("% d \ n ", array [I]) ;}}
Running result:
/** Pointer array and array pointer */void function4 (void) {char * s [10]; // pointer array char (* s1) [10]; // array pointer // The first expression means that there is an array containing 10 elements, each of which is a pointer variable pointing to the character array type. // So char c1 [2]; char c2 [3]; char c3 [4]; s [1] = c1; s [2] = c2; s [3] = c3; // The second is an array pointer, which is a pointer variable pointing to a string array of 10 characters. Char cc [2] [10]; // [Error] cannot convert 'Char [2] [5] 'to 'Char (*) [10] 'in assignment s1 = cc; s1 = & cc [1]; // understand these things tomorrow. int I; int * p; p = & I; // likewise: char mychars [10]; s1 = & mychars ;}