以下程序,都是在 C-Free5 的编译环境完成。
C语言 基础开发 ---- 目录
C语言基础入门:C-Free 5下载和安装详细教程
指针偏移
指针偏移 就是指针的加减运算,实现指针指向地址的偏移。
指针地址偏移量----计算
指针的地址偏移量和保存这个地址的指针类型有关。
指针的类型占有的内存空间,就是 指针加1 的地址偏移量。
数据类型 | 内存空间(字节) | 指针加1的地址偏移量 |
---|---|---|
char | 1 | 1 |
short | 2 | 2 |
int | 4 | 4 |
float | 4 | 4 |
double | 8 | 8 |
具体代码如下:
#include <stdio.h>
int main(void)
{
char *c;
printf("char = %d\n",sizeof(char));
printf("0x%p\n",c);
printf("0x%p\n\n",c+1);
short *s;
printf("short = %d\n",sizeof(short));
printf("0x%p\n",s);
printf("0x%p\n\n",s+1);
int *i;
printf("int = %d\n",sizeof(int));
printf("0x%p\n",i);
printf("0x%p\n\n",i+1);
float *f;
printf("float = %d\n",sizeof(float));
printf("0x%p\n",f);
printf("0x%p\n\n",f+1);
double *d;
printf("double = %d\n",sizeof(double));
printf("0x%p\n",d);
printf("0x%p\n\n",d+1);
return 0;
}
运行结果如下: