// 24T3 COMP1511 Week 5 Lecture 2 // Introduction to basic pointer concepts // Address of operator & // Declaring and initialised pointer variables // Dereferencing pointer variables #include int main(void) { // Decare and initialise a normal int variable called 'x' int x = 2; // Print the address and value of x printf("The int that x stores (x): %d\n", x); printf("The address of x (&x): %p\n", &x); // // Declare a pointer variable 'x_ptr' and assign the address of 'x' to it int *x_ptr = &x; // Print the addresses and value of x_ptr printf("The address that x_ptr stores (x_ptr): %p\n", x_ptr); printf("The address of x_ptr (&x_ptr): %p\n", &x_ptr); // Dereference 'x_ptr' printf("The int we get when we dereference x_ptr (*x_ptr): %d\n", *x_ptr); // Modify x indirectly via our pointer *x_ptr = 99; printf("x:%d *x_ptr:%d \n", x, *x_ptr); // // make num_ptr points to where x_ptr is pointing at int *num_ptr = x_ptr; // // // What does num_ptr contain printf("num_ptr: %p x_ptr: %p &x: %p\n", num_ptr, x_ptr, &x); // Indirectly modify x via num_ptr *num_ptr = 10; // x = 10; printf("x: %d *x_ptr: %d *num_ptr: %d\n", x, *x_ptr, *num_ptr); // num_ptr is pointing somewhere else now int y = 4; num_ptr = &y; printf("*num_ptr %d\n", *num_ptr); y = 99; printf("*num_ptr %d\n", *num_ptr); // // INVALID C CODE //double d = 1.5; //num_ptr = &d; num_ptr = NULL; printf("%p\n", num_ptr); printf("%d\n", *num_ptr); if (num_ptr != NULL) { printf("%p %d\n", num_ptr, *num_ptr); } return 0; }