Table Of Contents
Previous Section Next Section

Program 28: Zero Error

The program is designed to zero out an array. So why doesn't it work? Is memset broken?

  1 /************************************************
  2  * zero_array -- Demonstrate how to use memset  *
  3  *      to zero an array.                       *
  4  ************************************************/
  5 #include <iostream>
  6 #include <cstring>
  7
  8 int main()
  9 {
 10     // An array to zero
 11     int array[5] = {1, 3, 5, 7, 9};
 12
 13     // Index into the array
 14     int i;
 15
 16     // Zero the array
 17     memset(array, sizeof(array), '\0');
 18
 19     // Print the array
 20     for (i = 0; i < 5; ++i)
 21     {
 22         std::cout << "array[" << i << "]= " <<
 23             array[i] << std::endl;
 24     }
 25     return (0);
 26 }

(Next Hint 50. Answer 20.)

Table Of Contents
Previous Section Next Section