website/code/cheri/ptrs_as_numbers.c
2023-01-06 03:04:10 +00:00

22 lines
540 B
C

#include <stdio.h>
int main() {
int magic = 9999;
(void)magic; // suppress unused warning
int arr[] = { 1234, 5678 };
int *x = &(arr[0]); // x is a pointer to first element of arr
printf("*(%lx)=%d\n", (size_t) x, *x);
unsigned long x_addr = (size_t) x; // we're going to assume size_t = unsigned long here
x_addr += 4; // sizeof(int) == 4
x = (int *) x_addr;
printf("*(%lx)=%d\n", (size_t) x, *x);
x_addr += 4;
x = (int *) x_addr;
printf("*(%lx)=%d\n", (size_t) x, *x);
return 0;
}