![](/img/trans.png)
[英]Why does the dereference operator fail to return value pointed by a “pointer to an array” variable?
[英]Why does a single pointer act like an array and the dereference operator (*) isn´t needed to access the pointed element?
int *insertZeroPosition(int *pf,int n,int k){
int *ptr=(int *)calloc(n+1,sizeof(int));
int i;
ptr[0]=k;
for (i=1;i<n+1;i++,pf++){
ptr[i]=*pf;
}
return ptr;
}
為什么它是ptr[i]=*pf
而不是*ptr[i]=*pf
即使 ptr 是一個指針?
語法p[i]
等價於*(p + i)
。 即使使用數組下標語法,取消引用仍然存在。
C 標准,第 6.5.2.1.2 節:
下標運算符[]的定義是E1[E2]等同於(*((E1)+(E2)))。
如果需要,您可以將代碼重寫為*(ptr + i) = *pf
; 沒有區別。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.