Chapter 13

I l @ ve RuBoard

Chapter 13

  1. The automatic and the static storage classes.

  2. The external storage class, the external static storage class, and the static storage class.

  3. The external storage class and the external static storage class.

  4. One source of inefficiency is that items are often relocated only to be relocated again before the inner loop finishes a cycle. Also, sometimes they actually get moved to positions far worse than the original position. Suppose, for example, that you're sorting the values 2 , 10 , 14 , 8 , 6 , 19 , and 1 in increasing order. At the end of the first inner loop cycle, the 1 and 2 are swapped, positioning the 1 correctly but placing the 2 far from its final position.

  5. Replace array[search] > array[top] with array[search] < array[top] .

  6. A string should be stored in a character array, so the array argument should be a pointer to an array of characters; the size of each character array depends on how many characters will be in the hexadecimal number. Let's assume that 20 is sufficient:

     int getarray(char array[][20], int limit) 

    Because the data type is a string, use the %s specifier instead of %d for scanf() and printf() .

    You would have to alter the test for a valid number. The new test should examine the string character by character to see whether they are hexadecimal digits; you can apply the ctype .h function isxdigit () to test each character for that purpose. (If you want to allow for the C-code notation of using a 0x or 0X prefix, you'd need to add extra code to check for that.)

  7. daisy is known to main() by default, and to petal() , stem() , and root() because of the extern declaration. The extern int daisy; declaration in file 2 makes daisy known to all the functions in file 2. The first lily is local to main() : The reference to lily in petal() is an error because there is no external lily in either file. There is an external static lily , but it is known just to functions in the second file. The first external rose is known to root() , but stem() has overridden it with its own local rose .

  8. Here is the output:

     color in main() is B color in first() is R color in main() is B color in second() is G color in main() is G 
    1. It tells us that the program will use a variable plink that is local to the file containing the function. The first argument to value_ct() is a pointer to an integer, presumably the first element of an array of n members . The important point here is that the program will not be allowed to use the pointer arr to modify values in the original array.

    2. No. Already value and n are copies of original data, so there is no way for the function to alter the corresponding values in the calling program. What these declarations do accomplish is to prevent the function from altering value and n within the function. For example, the function couldn't use the expression n++ if n were qualified as const .

I l @ ve RuBoard


C++ Primer Plus
C Primer Plus (5th Edition)
ISBN: 0672326965
EAN: 2147483647
Year: 2000
Pages: 314
Authors: Stephen Prata

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net