Pointer and Struct Study Sheet The fundamental concepts you need to know 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 or does it contain garbage or NULL? These concepts are true whether we are working with pointers to int, float, double, arrays, or structs. NOTE: When we are working with parameters, as in the command line parameters for main(int argc, char *argv[]), the behavior of argv INSIDE the function is the same as if argv were a parameter listed as char argv[][SIZE] for some SIZE constant. However, the declarations char a[SIZE][SIZE]; char *a[SIZE]; are very different. The first declares a two-D array of char: space for SIZE strings SIZE long each. The second declares a 1-D array of pointers, with no space. We can use it in a parameter list because someone else already made the space and is passing us the address (or we can declare our own space dynamically). 1) For each statement, show the effect by writing what is printed, drawing the effect in memory, or writing "error". 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 in memory, 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 3. Give the types of all the identifiers declared here. a. struct S { int A; char business[30]; }; b. struct S Z; c. struct S *X; 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 "drugstore" in member business of Z. Change the letter u to a in member business of Z. 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. 8. Explain when strcmp, strcat, and strcpy should be used. 9. Write the function "swap" to switch the values of two variables in main. Show how it is called. Then do the same, passing an array and two indices.