Chapter 10

I l @ ve RuBoard

Chapter 10

  1. The printout is this:

     D D O O L L T T 
  2. The array ref has an external storage class; it is a global variable with external linkage.

  3. The array name ref points to the first element of the array, the character D . The expression ref + 1 points to the second element, the character O . The construction ++ref is not a valid C expression; ref is a constant, not a variable.

  4. ptr points to the first element, and ptr + 2 points to the third element, which would be the first element of the second row.

    1. 12 and 16

    2. 12 and 14 (just the 12 goes in the first row because of the braces)

  5. ptr points to the first row and ptr+1 points to the second row; *ptr points to the first element in the first row, and *(ptr + 1) points to the first element of the second row.

    1. 12 and 16

    2. 12 and 14 (just the 12 goes in the first row because of the braces)

    1. &grid[22][56]

    2. &grid[22][0] or grid[22]

      (The latter is the name of a one-dimensional array of 100 elements, hence the address of its first element, which is the element grid[22][0] .)

    3. &grid[0][0] or grid[0] or (int *) grid

      (Here grid[0] is the address of the int element grid[0][0] , and grid is the address of the 100-element array grid[0] . The two addresses have the same numeric value but different types; the typecast makes the types the same.)

    1. int digits[10];

    2. float rates[6];

    3. int mat[3][5];

    4. char (*pstr)[20];

      Note

      char *pstr[20]; is incorrect. This would make pstr an array of pointers instead of a pointer to an array. In particular, pstr would point to a single char , the first member of the array; pstr + 1 would point to the next byte. With the correct declaration, pstr is a variable rather than an array name , and pstr + 1 points 20 bytes beyond the initial byte.


    5. char * psa[20]; . Note that the [] have higher precedence than * , so in the absence of parentheses, the array descriptor is applied first, then the pointer descriptor. Hence, this declaration is the same as char *(psa[20]); .

    1. int sextet[6] = {1, 2, 4, 8, 16, 32};

    2. sextet[2]

  6. 0 through 9

    1. rootbeer[2] = value;

      Valid

    2. scanf("%f", &rootbeer );

      Invalid; rootbeer is not a float .

    3. rootbeer = value;

      Invalid; rootbeer is not a float .

    4. printf("%f", rootbeer);

      Invalid; rootbeer is not a float .

    5. things[4][4] = rootbeer[3];

      Valid

    6. things[5] = rootbeer;

      Invalid; can't assign arrays.

    7. pf = value;

      Invalid; value is not an address.

    8. pf = rootbeer;

      Valid

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