//file: oct1.c //author: jhd //This code adapts pointer code from Tanenbaum and Bos, Ch 1, section 1.8, p75. //It tries to demo pointers as placed in memory, in the data blocks of certain addresses. #include #include int main(){ printf("Hello: \n"); //declarations of variables char c; void *p = &c; int i1, i2; void *p1, *p2; int *p3; //variables have addresses in memory p = &c; printf("Declare a variable with type and name char c. \nDeclare a void pointer variable named *p.\nSet pointer p equal to address of c, &c.\n"); printf("Use percent p to print the pointer containing the addr of var c: p = &c = %p\n", p); //set i1 i1 = 1; printf("The var i1 has data, so i1 = %d\n", i1); //store address of i1 in void pointer p1 and copy it to a pointer of type int p1 = &i1; p3 = (int *)p1; printf("The pointer p1 has data, address of var i1, so p1 = %p\n", p1); printf("The pointer p3 has data, address of var i1, so p3 = %p\n", p3); //dereference (get data from) p3 (you cannot deref a void ptr!) i2 = *p3; printf("The var i2 now has data, the contents of variable pointed to by p3, i2 = *p3 = %d\n", i2); printf("Hello: Item: Address: Data: \n"); exit(0); }