30 lines
1.1 KiB
C
30 lines
1.1 KiB
C
/*
|
|
* How far is p + 1? Given; do not edit it. Predict first, then run it.
|
|
* README.md says how to build and run it.
|
|
*/
|
|
#include <stdio.h>
|
|
|
|
int main(void)
|
|
{
|
|
char letters[4] = {'a', 'b', 'c', 'd'};
|
|
int counts[4] = {10, 20, 30, 40};
|
|
double weights[4] = {0.5, 1.5, 2.5, 3.5};
|
|
|
|
char *pc = letters;
|
|
int *pi = counts;
|
|
double *pd = weights;
|
|
|
|
/* A char is one byte, so the gap between two char * addresses is in bytes. */
|
|
printf("char: p = %p p + 1 = %p bytes apart: %td\n",
|
|
(void *)pc, (void *)(pc + 1), (char *)(pc + 1) - (char *)pc);
|
|
printf("int: p = %p p + 1 = %p bytes apart: %td\n",
|
|
(void *)pi, (void *)(pi + 1), (char *)(pi + 1) - (char *)pi);
|
|
printf("double: p = %p p + 1 = %p bytes apart: %td\n",
|
|
(void *)pd, (void *)(pd + 1), (char *)(pd + 1) - (char *)pd);
|
|
|
|
/* The same moves, read back through the pointers. */
|
|
printf("*(pc + 1) = %c *(pi + 1) = %d *(pd + 1) = %.1f\n",
|
|
*(pc + 1), *(pi + 1), *(pd + 1));
|
|
printf("(pi + 3) - pi = %td\n", (pi + 3) - pi);
|
|
return 0;
|
|
}
|