website/code/cheri/ptrs_as_numbers.c

22 lines
540 B
C
Raw Normal View History

2022-11-19 11:56:01 +00:00
#include <stdio.h>
int main() {
int magic = 9999;
2022-11-22 00:13:56 +00:00
(void)magic; // suppress unused warning
2022-11-19 11:56:01 +00:00
int arr[] = { 1234, 5678 };
int *x = &(arr[0]); // x is a pointer to first element of arr
2022-11-22 00:13:56 +00:00
printf("*(%lx)=%d\n", (size_t) x, *x);
2022-11-19 11:56:01 +00:00
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;
2022-11-22 00:13:56 +00:00
printf("*(%lx)=%d\n", (size_t) x, *x);
2022-11-19 11:56:01 +00:00
x_addr += 4;
x = (int *) x_addr;
2022-11-22 00:13:56 +00:00
printf("*(%lx)=%d\n", (size_t) x, *x);
2022-11-19 11:56:01 +00:00
return 0;
}