Decay就是當變數要傳給function的時候
像是a[N] 傳進去function只要傳他的第一個位置
int (*)[N]的意思是 有一個poionter指向有[N]個的int
int* (*)[N]有一個pointer指向有[N]個int *
char g[8]="456";
char (*ptr2)[8]="234"; -->(*ptr2)是一個指標指向一個長度為8的char (這邊指派一個唯讀的"234"給他)
ptr2=&g; -->因為ptr2是指標 所以可以
但是如果一開始只有宣告char (*ptr2)[8];
是不能做
(*ptr2)[0]='G';
必須要是
char t='G';
(*ptr2)[0]=t; -->要給一個實體記憶體位置
So the following all hold true:
Declaration Expression Type Decays to
----------- ---------- ---- ---------
T a[N] a T [N] T *
&a T (*)[N]
*a T
a[i] T
T a[M][N] a T [M][N] T (*)[N]
&a T (*)[M][N]
*a T [N] T *
a[i] T [N] T *
&a[i] T (*)[N]
*a[i] T
a[i][j] T
T a[M][N][O] a T [M][N][O] T (*)[M][N]
&a T (*)[M][N][O]
*a T [M][N] T (*)[N]
a[i] T [M][N] T (*)[N]
&a[i] T (*)[M][N]
*a[i] T [N] T *
a[i][j] T [N] T *
&a[i][j] T (*)[N]
*a[i][j] T
a[i][j][k] T
The pattern should be clear for higher-dimensional arrays. So let's analyze your dictionary:
/* list of words and meanings */
char *dic[][40] = {
"atlas", "A volume of maps.",
"car", "A motorized vehicle.",
"telephone", "A communication device.",
"airplane", "A flying machine.",
"", "" /* null terminate the list */
};
This isn't going to set up your dictionary the way you want; you've basically set this up as a 1-element array of 40 pointers to char. If you want an array of pairs of strings, then the declaration should look like this:char *dic[][2] =
{
{"atlas", "A volume of maps"},
{"car", "A motorized vehicle"},
{"telephone", "A communication device"},
{"airplane" , "A flying machine"},
{NULL, NULL} // empty strings and NULLs are different things.
};
The type of dic
is "5-element array of 2-element arrays of pointer to char", or char *[5][2]
. Going by the rules above, the expression dic
should decay to char *(*)[2]
-- a pointer to a 2-element array of pointer to char. A function to search this dictionary would then look like this:
char *definition(char *term, char *(*dictionary)[2]) // *NOT* char ***dictionary
{
while ((*dictionary)[0] != NULL && strcmp((*dictionary)[0], term) != 0)
dictionary++;
return (*dictionary)[1];
}
and you would call it from your main function likechar *def = definition(term, dic);
Note that we have to use parentheses around the *dictionary
expression in the function. The array subscript operator []
has higher precedence than the dereference operator *
, and we don't want to subscript into dictionary
directly, we want to subscript into the array that dictionary
points to.------------------------
How to create an array size 5 containing pointers to characters:
char *array_of_pointers[ 5 ]; //array size 5 containing pointers to char
char m = 'm'; //character value holding the value 'm'
array_of_pointers[0] = &m; //assign m ptr into the array position 0.
printf("%c", *array_of_pointers[0]); //get the value of the pointer to m
How to create a pointer to an array of characters:char (*pointer_to_array)[ 5 ]; //A pointer to an array containing 5 chars
char m = 'm'; //character value holding the value 'm'
*pointer_to_array[0] = m; //dereference array and put m in position 0
printf("%c", (*pointer_to_array)[0]); //dereference array and get position 0
How to create an 2D array containing pointers to characters:char *array_of_pointers[5][2];
//An array size 5 containing arrays size 2 containing pointers to char
char m = 'm';
//character value holding the value 'm'
array_of_pointers[4][1] = &m;
//Get position 4 of array, then get position 1, then put m ptr in there.
printf("%c", *array_of_pointers[4][1]);
//Get position 4 of array, then get position 1 and dereference it.
How to create a pointer to an 2D array of characters:char (*pointer_to_array)[5][2];
//A pointer to an array size 5 each containing arrays size 2 which hold chars
char m = 'm';
//character value holding the value 'm'
(*pointer_to_array)[4][1] = m;
//dereference array, Get position 4, get position 1, put m there.
printf("%c", (*pointer_to_array)[4][1]);
//dereference array, Get position 4, get position 1
---------------------
Here's an example if char a[N][3]
:
+===========================+===========================+====
|+--------+--------+-------+|+--------+--------+-------+|
|| a[0,0] | a[0,1] | a[0,2]||| a[1,0] | a[1,1] | a[1,2]|| ...
|+--------+--------+-------+++--------+--------+-------++ ...
| a[0] | a[1] |
+===========================+===========================+====
a
^^^
||+-- &a[0,0]
|+-----&a[0]
+-------&a
&a
: address of the entire array of arrays of chars, which is a char[N][3]
&a[0]
, same as a
: address of the first element, which is itself a char[3]
&a[0][0]
: address of the first element of the first element, which is a char
-----------------
http://stackoverflow.com/questions/6599707/2d-character-array-initialization-in-c
http://stackoverflow.com/questions/3920729/in-c-c-is-char-arrayname-a-pointer-to-a-pointer-to-a-pointer-or-a-pointe
http://stackoverflow.com/questions/12674094/array-to-pointer-decay-and-passing-multidimensional-arrays-to-functions?answertab=oldest#tab-top
沒有留言:
張貼留言