1 #include <stdio.h> 2 #include <malloc.h> 3 4 int main() 5 { 6 char s1[] = {‘H‘, ‘e‘, ‘1‘, ‘2‘, ‘o‘}; 7 char *p=(char *)(s1+3); 8 printf("*p:%c\n",p[0]); 9 char *p2=(char *)(&s1[0]+4); 10 printf("*p2:%c\n",p2[0]); 11 char *p3=(char *)(&s1+1); 12 printf("*p3:%c\n",p3[-1]); 13 14 return 0; 15 }
输出:
*p:2
*p2:o
*p3:o
在这之前,取数组值时老是不能区分s1+2,&s1[0]+2,&s1+2之间的区别,今天特意测试了下,原来他们的区别如下:
s1+2:
指从s1数组0位开始向下移动2位的取值。
&s1[0]+2:
同s1+2一样,指从s1数组0位开始向下移动2位的取值。
特别注意点:&s1+1:
指的是移到s1整个数组的下一个位置,相当于移出了s1数组。
原文:http://www.cnblogs.com/ltlly/p/4225641.html