|

楼主 |
发表于 2004-5-22 22:35:21
|
显示全部楼层
是我把指针和指针变量搞混了,所以讲了很多大家也没明白我想干吗
指针的数据类型决定了-1的实际数值;
指针的数据类型就是指针指向的数据的类型,指针实际上就是这个数据的地址;
指针变量则是保存指针(地址)的那个变量;它的数据类型则是整型的。
再完善的改造一下演示程序,如下:
- [linuxer@mydesktop bmp]$ cat test.c
- /*
- * 演示指针赋值,和运算的程序。
- * %p和%x输出格式化只是%p带前导0。
- * Version 0.4.0
- * Copyright (C) seablue at linuxsir.cn
- * 2004-05-23
- */
- #include <stdio.h>
- main()
- {
- short *ptr;
- int *lptr;
- int a,b,c,d;
- ptr=(short *)0x0002;
- lptr=(int *)0x00000004;
- a=(int)ptr;
- b=(int)(ptr-1);
- c=(int)lptr;
- d=(int)(lptr-1);
- printf("ptr=%lx, ptr-1=%lx\n",ptr,ptr-1);
- printf("lptr=%lx, lptr-1=%lx\n",lptr,lptr-1);
- printf("ptr-1(指针变量减1)实际减少的字节数:%ld\n",a-b);
- printf("lptr-1(指针变量减1)实际减少的字节数:%ld\n",c-d);
- }
- 结果是:
- [linuxer@mydesktop bmp]$ gcc -o test.o test.c
- [linuxer@mydesktop bmp]$ ./test.o
- ptr=2, ptr-1=0
- lptr=4, lptr-1=0
- ptr-1(指针变量减1)实际减少的字节数:2
- lptr-1(指针变量减1)实际减少的字节数:4
复制代码 |
|