數組始終使用從0開始的索引。
例如:
int my_arr[] = { 10, 11, 12, 13, 14, 15 };
意味着數組my_arr具有從索引0開始直到6-1 6個元件(5)。所以,一個N元素數組的索引從0到N-1。另外請注意,my_arr本身也是一個指向數組的指針。
限定INT指針:
int *iPtr = my_arr; // note that we do not use &my_arr here. my_arr is already a pointer to the array it defined.
或
int *iPtr;
iPtr= my_arr;
它們是相同的。
在圖中,框所代表的價值和它們的地址除了框寫入。
當您使用變量i
作爲索引遍歷數組時。你基本上做間接如下:
i = 0
my_arr[i]
是相同
my_arr[0]
是相同
*(my_arr + 0)
是一樣的(替代命名my_arr框的值)
*(20014000+0)
*運算符讓你的VA在此示例中的地址0x20014000
處爲10
。
上述操作也一樣
iPtr[0] or *(iPtr+0)
看看命名iPtr
盒子,它也包含相同的值0x20014000
。
現在,讓我們定義一個指針數組:
int *myptr_arr[3];
int varA = 25;
int varB = 34;
int varC = 67;
myptr_arr[0] = &varA; // note that varA is not a pointer, to get its address use &
myptr_arr[1] = &varB; // note that varB is not a pointer, to get its address use &
myptr_arr[2] = &varC; // note that varC is not a pointer, to get its address use &
再次,當你寫myptr_arr[0]
將其轉換爲
*(myptr_arr + 0)
*(20152000 + 0)
這將讓你0x20162000
即價值框名爲[0]
=地址varA
。現在要獲取此地址的值,您需要使用*
運算符對其進行解引用。
*myptr_arr[0]
其是相同* (*(myptr_arr + 0))
其是相同*(0x20162000)
這是框varA
的值,請參見:Example usage of pointers
'ST [I]'是'字符*',或一個指向角色的指針。 '&str'是一個char(*)[120]',或者一個指向120個字符數組的指針。注意那些是不同的。 – immibis
所以我需要改變&str str,當我這樣做時,我得到了這個:12 ! ! –
'int i = 0;'..'st [i] = strdup( str);''if(i == 2)'..'for(i = 0; i <2; i ++){' – BLUEPIXY