Pencil-and-Paper Assignment 5 Assigned Monday, February 11, 2002 Due Monday, February 18, 2002 (At the beginning of class) ============================================================================ Solutions ============================================================================ 1. Given the following declaration, int a[] = {1,2,3,4,5}; what are the values of the expressions below? (a) a - &a[3] = -3 (b) *(a+4) = 5 (c) *(a+5) - accesses the address beyond the end of the array - indeterminate 2. Explain the meaning of each of the following declarations: (a) char a[]; // creates a char[] named a, allocates no space (b) char *b; // creates a char * named b, allocates no space (c) char c[5] = ""; // creates a char[] named c, allocates 5 spaces plus one // for the null character, initializes it to an empty string (d) int d[] = {0,1,2}; // creates an int[] named d, reserves 3 spaces and // initializes the 3 values (e) int *e = d+1; // creates a pointer to an integer named d and // initializes it to point to the second element in array d (f) char *g[]; // Creates an array of character pointers named g, no // space is allocated (g) char *g[] = {a,b,c}; // Creates an array of character pointers named g, // allocates space for 3 pointers and initializes them // to the addresses stored in a, b and c (h) char **a; // creates a pointer to a pointer to a char, named a (i) char **a = &b; // creates a pointer to a pointer to a char named a // and assigns to it the address of variable b - b // should be a char * (j) typedef int (* C_FN)(char *, const char *); // defines C_FN as a type that // is a pointer to a function that takes a char * and // a const char * as parameters and returns an int 3. Given that k is an integer array starting at location 2000, kPtr is a pointer to k, and each integer is stored in 4 bytes of memory: (a) If the location is 0x2000, then kptr+5 points to 20 positions pas 0x2000, which is 0x2014 The second element of k is: (b) k[1] (c) *(k+1) (d) kptr[1] (e) *(kptr+1) (e) pointer/offset notation 4. Consider the following function: void func( char * string1, const char * string2 ) { int stringsize = sizeof( string1 )/sizeof( char ); *( string1 + stringsize -1 ) = ‘\0’; string1 = string1 + stringsize - 2; for ( ; *string2 != '\0'; string1--, string2++ ) *string1 = *string2; } sizeof(string1) is usually 4 (4 bytes for a pointer), sizeof(char) is usually 1, so, despite what we might think, stringsize is 4, not the number of characters in the array. The next line puts a null terminator after the 3rd byte in string1 The last two lines copy 3 characters from string2 into string1 in reverse order - but it doesn't work because it doesn't stop when it reaches the beginning of string1 5. Provide the definition for each of the following structures. (a) A structure called Address that contains character arrays streetAddress[ 25 ], city[ 20 ], state[ 3 ] and zipCode[ 6 ]. struct Address { char streetAddress[25]; char city[20]; char state[3]; char zipCode[6]; }; (b) Structure Student that contains arrays firstName[ 15 ] and lastName[ 15 ], integer id, floating-point GPA, and variable homeAddress of type struct Address from part (a). struct Student { char firstName[15]; char lastName[15]; integer id; float GPA Address homeAddress; } 6. Say that you have the following in your program: int i; int *ip; int **ipp; i = 4000; ip = &i; ipp = &ip; Say that i is stored at memory location 0x606EF0, ip is stored at memory location 0x606EF8 and ipp is stored at memory location 0x606F08. a) Draw a map of the memory and fill in the values of each variable. variable: location: contents: i: 0x606EF0 4000 ip: 0x606EF8 0x606EF0 ipp: 0x606F08 0x606EF8 b) What is the value of each of the following (some are not even logically valid expressions - note which ones are not): i = 4000, &i = 0x606EF0, *i not valid, &ip = 0x606EF8 ip = 0x606EF0, *ip = 4000, **ip not valid, &*ipp=0x606EF8 *ipp = 0x606EF0, **ipp = 4000, &&i not valid, &ipp = 0x606F08. 7. Do problem 6.12 in Deitel and Deitel class Rectangle { public: Rectangle(float =1, float=1); float perimeter(); float area(); void set_width(float); void set_length(float); float get_width(); float get_length(); private: float width; float length; }; Rectangle::Rectangle(float w, float l) { set_width(w); set_length(l); } float Rectangle::perimeter() { return (2*width + 2*length); } float Rectangle::area() { return length*width; } void Rectangle::set_width(float w) { if (w>0 && w < 20) width = w; else width = 1; } void Rectangle::set_length(float l) { if (l>0 && l < 20) length = l; else length = 1; } float Rectangle::get_width() { return width; } float Rectangle::get_length() { return length; } 8. Do problem 16.7 in Deitel and Deitel a) customerRecord.lastName b) customerPtr->lastNmae c) customerRecord.firstName d) customerPtr->firstName e) customerRecord.customerNumber; f) customerPtr->customerNumber; g) customerRecord.personal.phoneNumber h) customerPtr->personal.phoneNumber i) customerRecord.personal.address j) customerPtr->personal.address k) customerRecord.personal.city l) customerPtr->personal.city m) customerRecord.personal.state n) customerPtr->personal.state o) customerRecord.personal.zipcode p) customerPtr->personal.zipcode