home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The C Users' Group Library 1994 August
/
wc-cdrom-cusersgrouplibrary-1994-08.iso
/
listings
/
v_11_08
/
1108110a
< prev
next >
Wrap
Text File
|
1993-06-08
|
683b
|
33 lines
/* 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 */