/* * Pointer demo - a la Binky * CSE 413 demo 10/15 rea */ #include int main() { int* x; // Allocate the pointers x and y int* y; // (but not the pointees) int z; x = malloc(sizeof(int)); // Allocate an int pointee, // and set x to point to it *x = 42; // Dereference x to store 42 in its pointee printf("Assign x to 42\n"); *y = 13; // CRASH (??) -- y does not have a pointee yet z = 45; printf ("x = %d y = %d z = %d\n", x, y, z); printf ("x = %p y = %p z = %p\n", x, y, z); printf ("*x = %d *y = %d\n", *x, *y); y = x; // Pointer assignment sets y to point to x's pointee *y = 13; // Dereference y to store 13 in its (shared) pointee y = &z; printf ("x = %d y = %d z = %d\n", x, y, z); *y = 16; printf ("*x = %d *y = %d\n", *x, *y); printf ("x = %d y = %d z = %d\n", x, y, z); return 0; }