一維數組 數組名相當于當前類型的指針 type* const &數組名
int array[10];int other[2][5];//內存分布一樣array+1;//首元素地址+1*type(4)other+1;//首 &數組名+5*1*type(4);+1相當于數組指針+1一個數組有 :數組類型 元素類型 數組大小
數組指針
int(*parray)[10]=&array;//數組指針與array 數據類型相同tydef int(INT10)[10];//sizSEOf =40INT10 intarray;二維參數 相當于傳遞數組指針
void foo(int (*array)[10]){}int other[2][10];foo(&array)//&array ->常量數組指針(int(*)[10])void foo(int (*array)[2][10]){}foo(other)//常量指針 other數組指針(int(*)[10]),int*10個元素的數組指針foo(&ohter)&other->常量數組指針(int(*)[2][10])指針數組
char *keys[100]={"aaa","bbb","ccc"};//能夠存放100個指針,與能存放多長字符串無關,存放100個char*的指針,指向char類型,//只讀,常量區char keys[100][100];//能夠存放100個字符串,長度100;代表字符數組,不是字符串,可讀寫,棧區指針數組傳參 char **key和char *key[]一樣 傳入指針的指針,
void SetArrayValue(char *key[],unsigned int index,char *value){ *(key + index) = value;//傳入指針的指針}函數指針 好處是可使用回調函數
int foo(int one,int two){return 0;}void *pfoo=&foo;//函數指針 函數類型pfoo(10,10);//調用 調用時和普通調用一樣函數類型:返回值 參數列表(類型,個數,順序) int(*pfoo)(int,int)=foo;//函數類型
void foo2(){}void(*pfoo2)()=&foo2;typedef int FOOTYPE(int,int);//可當參數使用FOOTYPE* pfoo=foo;void foo3(FOOTYPE *func){}foo3(pfoo);回調error
typedef void (FUNCERROR)(int)void PRintError(int code){ printf("Error:%d/r/n",code);}void SetValue(char *array[],char *value,FUNCERROR*error)if(array==NULL) error(100)指針,使用場合 作為參數 動態分配內存
//指針的指針void foo1(int **array,unsigned int size){*array=malloc(size *sizeof(int));}int *array=NULL;foo1(&array,100);free(array);數組類型:元素類型+元素個數 一維數組: &數組名:數組指針,數組類型*,tpye(name)[size] 數組名:當前元素地址,類型是元素的指針type*
二維數組: &數組名:數組指針,type(name)[一維][二維] 數組名:元素地址;類型:數組指針:type(name)[二維size] 指針數組: 數組名:首元素地址;類型:type(*)* &數組名:數組指針:type(*)(name)[size] 使用場合:參數傳遞:降維傳遞
函數指針: 返回值(name)(參數列表)
新聞熱點
疑難解答