Pointer and Struct Study Sheet The fundamental questions you need to ask about pointers are: What kind of value is in a pointer variable? How do I dereference a pointer to get the value that it points to? How do I get the address of any variable? Can any variable be dereferenced? Does this pointer contain the address of a place allocated in memory (on the stack or the heap) or does it contain garbage or NULL? These concepts are true whether we are working with pointers to int, float, arrays, or structs. 1) For each statement, show the effect by writing what is printed, drawing the effect on the stack and the heap, or writing "error". Make up stack and heap addresses as you draw, as we do in class. int x = 7, y; int *ptr1, *ptr2; ptr1 = &x; ptr2 = ptr1; printf("%p",ptr1); printf("%d\n", *ptr1); printf("%p\n" ptr2,); printf("%d\n", *x); printf("%d\n", *&x); 1a) For each statement, show the effect by writing what is printed, drawing the effect on the stack and the heap, or writing "error". int *a = (int*)malloc(sizeof(int)); char b[] = "spam"; char * c; a = &7; c = (char*)malloc(sizeof(char)); b[2] = 'i'; *c = b[0]; b = c; 1b) Write a single statement that will: char w[][] = {"the", "quick", "brown", "fox"}; a. Print out the second word b. Print the letter 'w' from "brown" c. Change the second word to "spam" d. Change the 'x' in "fox" to a 'p' 2. For the following declarations, draw a picture as we do in class so you can answer the questions. Make up numbers for addresses, then use them consistently. int x = 5; int *ptr = &x; What are the values of the following expressions? a. ptr b. *ptr d. ptr == &x e. &ptr f. *x g. *&x h. **&ptr 3. Give the types of all the identifiers declared here. Also try to say them out loud. a. typedef struct { int a; double d; } S; b. S z; c. S *x; d. S y[10]; e. S *u[10]; 4. Using the declarations from 3 above, write the C expression to do the following: Put the value 7 in member a of z. Put the value 13.5 in the third element of y. Make x point to the third element of y. Make the second element of u point to y[5]. Using x and u, put 13 into the struct pointed to by u[1]. 5. Write a short function with a void return value that can modify a value in main. Write a call to it from main. 6. Write a short function that takes an array as a parameter and changes a value in the array. 7. Write a short function that takes an array as parameter but cannot change any element of the array. 8. Explain when strcmp and strcpy should be used. 9. Write the function "swap" to switch the values of two variables in main. Show how it is called. Also useful to know: The name of an array is a pointer, but a pointer to what? int a[5]; // a[0] is an integer; // a is an int*; a pointer to int int b[5][3]; // b[0][0] is an integer; // b[0] is an int*; // b is an int**; (that is, a ptr to a ptr to int) int c[5][3][6]; // c[0][0][0] is an integer; // c[0][0] is an int*; // c[0] is an int**; // c is an int***; Do you see the pattern? Do you understand the pattern? ***************************** Was all of the above too easy? Looking for a challenge? Stretch yourself with the following which we have not covered in class: Write a struct definition that contains a pointer to its own type, e.g. a Node that contains a pointer to type Node. Hint: this may be hard with the typedef syntax we've been using, try the other syntax first, then figure out how to integrate the two. Declare two instances of type Node. Use the Node pointer in one to point to the other. With this logic, you can make a chain of Nodes called a linked list. This is a very powerful idea that provides the central data structure of the language LISP, widely used in Artificial Intelligence. Create a chain of 5 Nodes. Set the final Node's pointer to NULL. Use a while loop to print each Node in the chain. Get stuck? See the text's linked list chapter for ideas.