diff options
| author | Mike Vink <mike@pionative.com> | 2024-05-22 08:49:29 +0200 |
|---|---|---|
| committer | Mike Vink <mike@pionative.com> | 2024-05-22 08:49:29 +0200 |
| commit | 51169f5f9ab178a4ddfe9dac461405a71c9c0f94 (patch) | |
| tree | 0b6bb0c6c31ee27361b28e2c5993f362c1cc95e2 /2/3_int_arith/show-bytes.c | |
| parent | 77f19e4a89d8dec97930c5e237139734c5fb3365 (diff) | |
organise
Diffstat (limited to '2/3_int_arith/show-bytes.c')
| -rw-r--r-- | 2/3_int_arith/show-bytes.c | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/2/3_int_arith/show-bytes.c b/2/3_int_arith/show-bytes.c new file mode 100644 index 0000000..0860b8d --- /dev/null +++ b/2/3_int_arith/show-bytes.c @@ -0,0 +1,33 @@ +#include <stdio.h> + +typedef unsigned char *byte_pointer; + +void show_bytes(byte_pointer start, size_t len) { + int i; + for (i = 0; i < len; i++) + printf(" %.2x", start[i]); + printf("\n"); +} + +void show_int(int x) { + show_bytes((byte_pointer) &x, sizeof(int)); +} + + +void show_float(float x) { + show_bytes((byte_pointer) &x, sizeof(float)); +} + + +void show_pointer(void *x) { + show_bytes((byte_pointer) &x, sizeof(void *)); +} + +void test_show_bytes(int val) { + int ival = val; + float fval = (float) ival; + int *pval = &ival; + show_int(ival); + show_float(fval); + show_pointer(pval); +} |
