#include <stdio.h>

void dumpMem(void *start, int numBytes)
{
    printf("memory at %p:", start);
    char *p = (char *)start;
    while((unsigned long)p%8){ p--;  numBytes++;}   // align to 8 byte boundary
    for(int i=0; i<numBytes; i++) 
    {
        if( i%8 == 0 ) printf("\nAddr %p:", p+i);
        printf(" %02x", (unsigned int) (p[i] & 0xff));
    }
    putchar('\n');
}

int len;        // static allocation, protect them from stack overwrites
char *from, *to;

int main(void)
{
    unsigned int    declStart = 0xaaaaaaaa; // marker
    int             *x = (int *)   0xbbbbbbbbbbbbbbbb;
    double          *y = (double *)0xcccccccccccccccc;
    unsigned int    declEnd = 0xdddddddd;   // marker

    printf("Addr. of x: %p,\n      of y: %p\n", &x, &y);

    // This is all UB because the pointers are not
    // belonging to the same object. But it should
    // work on standard architectures.
    // All calls to dumpMem() therefore are UB, too.

    // Thinking of it, I'd be hard-pressed to find 
    // any defined behavior in this program.
    if( &declStart < &declEnd ) 
    {
        from = (char *)&declStart;
        to = (char *)&declEnd + sizeof(declEnd);
    }
    else
    {
        from = (char *)&declEnd;
        to = (char *)&declStart + sizeof(declStart);
    }
    len = to - from;

    printf("len is %d\n", len);
    printf("Memory after initializations:\n");
    dumpMem(from, len);

    x = (int *)&x;
    printf("\nMemory after assigning own address %p to x/*x: \n", &x);
    dumpMem(from, len);

    *x = 3;
    printf("\nMemory after assigning 3 to x/*x: \n");
    dumpMem(from, len);

    //print val of pointer
    printf("x as long: %d\n", (unsigned long)x);

    y = (double *)&y;
    *y = 4.0;
    printf("\nMemory after assigning 4.0 to y/*y: \n");
    dumpMem(from, len);

    printf("y as float: %f\n", y);
    printf("y as double: %lf\n", y); // prints 0.0
    printf("y, cast as double: %lf\n", *(double *)&y); // prints 4.0
    printf("y as unsigned int: 0x%x\n", y);
    printf("y as unsigned long: 0x%lx\n", y);

    return 0;
}