logo

Courses with this lesson

printf(MEMORY) | How to print memory to the console

#include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct Thing { int test; char k; char str[10]; int* p; short sh; } Thing; int main(int argc, char* argv[]) { int i; Thing t = { 12, 'k', "testing", &i, 256 }; printf("Size: %llu\n", sizeof(t)); unsigned char data; for (i = 0; i < sizeof(t); i++) { if (i % 4 == 0) { printf("\n"); } data = *(((unsigned char*) &t) + i); printf("%02x ", data); } printf("\n"); for (i = 0; i < sizeof(t); i++) { if (i % 4 == 0) { printf("\n"); } data = *(((unsigned char*) &t) + i); printf("%03hhu ", data); } return 0; }