STM32C语言指针
char *s = (char *)"abcdefghijklmn";
double *s1 =(double *) s;
long *s2 =(long *) s;
int *s3 =(int *) s;
s1++;
s2++;
s3++;
printf("%c",*(char *)s1);
printf("%c",*(char *)s2);
printf("%c",*(char *)s3);
char *s = (char *)"abcdefghijklmn"; //s是字符指针,指向字符串首地址
double *s1 =(double *) s; //s1是 double类型指针,指向s首地址,也就是字符a存放地址。
long *s2 =(long *) s; //s2是 long类型指针,指向s首地址,也就是a存放地址
int *s3 =(int *) s; //s3是 int类型指针,指向s首地址,也就是a存放地址
s1++; //s1是double类型指针,8字节,所以地址+8,
s2++; //s1是long 类型指针,4字节,所以地址+4,
s3++; //s1是int 类型指针,4字节,所以地址+4,
printf("%c",*(char *)s1); //i
printf("%c",*(char *)s2); //e
printf("%c",*(char *)s3); //e
答案请看代码注释:
这里以double为例来解释:
首先s是指向字符串的字符指针,然后s1指向s,因为s1是double类型指针,所以s1++是地址+8,abcdefghijklmn。