CLASS EXERCISE C23
Determine the output of the pointer programs P1, P2, and P3.
/* P1.C illustrating pointers */ #include <stdio.h> main() { int count = 10, x, *int_pointer; /* this assigns the memory address of count to int_pointer */ int_pointer = &count; /* assigns the value stored at the address specified by int_pointer to x */ x = *int_pointer; printf("count = %d, x = %d\n", count, x); }
/* P2.C Further examples of pointers */ #include <stdio.h> main() { char c = 'Q'; char *char_pointer = &c; printf("%c %c\n", c, *char_pointer); c = '/'; printf("%c %c\n", c, *char_pointer); *char_pointer = '('; /* assigns ( as the contents of the memory address specified by char_pointer */ printf("%c %c\n", c, *char_pointer); } Answers
/* P3.C Another program with pointers */ #include <stdio.h> main() { int i1, i2, *p1, *p2; i1 = 5; p1 = &i1; i2 = *p1 / 2 + 10; p2 = p1; printf("i1 = %d, i2 = %d, *p1 = %d, *p2 = %d\n", i1, i2, *p1, *p2); } Answers