home *** CD-ROM | disk | FTP | other *** search
- /* arith.c: Illustrate pointer arithmetic */
- #include <stdio.h>
- #include <stddef.h>
-
- main()
- {
- float a[] = {1.0, 2.0, 3.0};
- float *p;
- ptrdiff_t diff;
-
- /* Increment a pointer */
- printf("sizeof(float) == %u\n",sizeof(float));
- p = &a[0];
- printf("p == %p, *p == %f\n",p,*p);
- ++p;
- printf("p == %p, *p == %f\n",p,*p);
-
- /* Subtract two pointers */
- diff = (p+1) - p;
- printf("diff == %d\n",diff);
- diff = (char *)(p+1) - (char *)p;
- printf("diff == %ld\n",(long)diff);
- return 0;
- }
-
- /* OUTPUT:
- * sizeof(float) == 4
- * p == FFEA, *p == 1.000000
- * p == FFEE, *p == 2.000000
- * diff == 1
- * diff == 4 */
-
-