title: Why does "The C Programming Language" book say I must cast malloc?
body: <p>Today I reached page <strong>167</strong> of <a href="https://rads.stackoverflow.com/amzn/click/com/0131103628" rel="noreferrer" rel="nofollow noreferrer">The C Programming Language (second edition Brian W. Kernighan &amp; Dennis  M. Ritchie)</a> and found that the author says I must cast <code>malloc</code>. Here is the part from the book:</p>
  
  <blockquote>
    <p><strong>7.8.5 Storage Management</strong></p>
    
    <p>The functions malloc and calloc obtain blocks of memory dynamically.</p>
  
  <pre><code>void *malloc(size_t n)
  </code></pre>
    
    <p>returns a pointer to n bytes of uninitialized storage, or NULL if the request cannot be satisfied.</p>
  
  <pre><code>void *calloc(size_t n, size_t size)
  </code></pre>
    
    <p>returns a pointer to enough free space for an array of n objects of the specified size, or NULL if
        the request cannot be satisfied. The storage is initialized to zero.
        The pointer returned by malloc or calloc has the proper alignment for the object in question,
        but it must be cast into the appropriate type, as in</p>
  
  <pre><code>int *ip;
  ip = (int *) calloc(n, sizeof(int));
  </code></pre>
  </blockquote>
  
  <p>I already know that <code>malloc</code> (and its family) returns type <strong>void*</strong>, and <a href="https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc">there are good explanations why not to cast <code>malloc</code></a>.</p>
  
  <p>But my question is: Why does the book say I should cast it?</p>