/* * SelfTest * Chapter 11 * By John Stile */ 1. For each of the following, write a single statement. 1a. Declare variable pnum to point to an object of type int ANSWER: int * pnum 1b. Assign the address of variable num to integer pointer pnum. ANSWER: pnum=# 1c. Print the value of the object pointed to by pnum. ANSWER: printf("%d\n", *pnum); 1d. Increment the value of the object pointed to by pnum by 10. aka add 10. ANSWER: *pnum+=10; 1e. Print the address of num using pnum. ANSWER: printf("%p\n", pnum); 1f. Change the value of num to 99 using pnum. ANSWER: *pnum=99; 2. Consider the following statment. char greeting[]="hello"; char *p=greetings; 2a. How many characters does the char array gretting contain? ANSWER: 6 (hello +'\0') 2b. What value does the character pointer have? ANSWER: p holds the value of &greeting[0] 2c. Make a pointer p get the address of the second element of greeting using p. ANSWER: printf("The second element of greeting is %c\n", *(p+1) ); 2d. Change the character 'o' to 'a' using p. ANSWER: *(p+4)=a 2e. Change all the characters in greeting to uppercase letters using a loop. for (ii=0; *(p+ii); ii++) // initialize position counter to 0 // while value-at pointer not NULL, do block // increment position counter { *(p+ii)-=32; // (value-at pointer) = (value-at pointer) - 32 } 3. Write a function named copystr that receives two pointers to arrays of strings with the same size. The first pointer str1 points to a string which has contents, but str2 points to a blank array. ANSWER: See SelfTest.question3.c