Problem thinking:
In a two-dimensional array, you cannot use the array name to express the first element of the array. The matrix no longer represents the address of the first element of the array.
Array type:
int array[5] is of type int[5], not int. INT[5] shows that this array has 5 elements, each element type is int.
To define an array type:
Array pointers:
Examples of array types and arrays of pointers:
1#include <stdio.h>2 3typedefint(AINT5) [5];4typedeffloat(AFLOAT10) [Ten];5typedefChar(ACHAR9) [9];6 7 intMain ()8 {9 AINT5 A1;Ten floatfarray[Ten]; Oneafloat10* PF = &Farray; A ACHAR9 CArray; - - Char(*PC) [9] = &CArray; the Char(*PCW) [4] =CArray; - - inti =0; - +printf"%d,%d\n",sizeof(AINT5),sizeof(A1)); - + for(i=0; i<Ten; i++) A { at(*PF) [I] =i; - } - - for(i=0; i<Ten; i++) - { -printf"%f\n", Farray[i]); in } - toprintf"%p,%p,%p\n", &carray, pc+1, pcw+1); + - return 0; the}
The type of line 15th does not match, and a warning is reported.
The results of the operation are as follows:
Array of pointers:
Example of a pointer array application in a project:
1#include <stdio.h>2#include <string.h>3 4 #defineDIM (a) (sizeof (a)/sizeof (*a))5 6 intLookup_keyword (Const Char* Key,Const Char* table[],Const intsize)7 {8 intRET =-1;9 Ten inti =0; One A for(i=0; i<size; i++) - { - if(strcmp (key, table[i]) = =0 ) the { -RET =i; - Break; - } + } - + returnret; A } at - intMain () - { - Const Char* keyword[] = { - " Do", - " for", in "if", - "Register", to "return", + "Switch", - " while", the " Case", * "Static" $ };Panax Notoginseng -printf"%d\n", Lookup_keyword ("return", keyword, DIM (keyword))); theprintf"%d\n", Lookup_keyword ("Main", keyword, DIM (keyword))); + A return 0; the}
The parameter in line 6th, const char* table[], is an array of pointers. The same as the char * argv[] of the main function.
The results of the operation are as follows:
Summary:
32nd lesson array pointer and pointer array analysis