Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The pointer cast is completely unnecessary. If your program appears to work, it's an unlucky coincidence. (I say "unlucky" because it means the error is not being diagnosed.)</p> <p><code>localtime()</code> takes an argument of type <code>const time_t*</code>, <em>not</em> <code>clock_t*</code>. <code>time_t</code> and <code>clock_t</code> are distinct types, used for different purposes, even though they <em>might</em> happen to be defined in the same way in some particular implementation.</p> <p>Change the definition of <code>now</code> to:</p> <pre><code>time_t now; </code></pre> <p>and call <code>localtime</code> like this:</p> <pre><code>mac_time = localtime(&amp;now); </code></pre> <p>Any cast, especially a pointer cast, should be viewed with suspicion. Casts are too often used to silence compiler warnings, when the real solution is to fix your code so things are declared with the correct type in the first place.</p> <p>As for the question you're asking -- well, it doesn't matter much, since the code is fundamentally incorrect. This:</p> <pre><code>mac_time = localtime((const long *)&amp;now) </code></pre> <p>takes the address of <code>now</code> (which, in your code, is of type <code>clock_t*</code>) and converts it to <code>const long*</code>. Since <code>localtime</code> expects an argument of type <code>const time_t*</code>, and <em>apparently</em> <code>time_t</code> is defined as <code>long</code> on your implementation, this <em>appears</em> to work.</p> <p>This:</p> <pre><code>mac_time = localtime(&amp;(const long *)now); </code></pre> <p>takes the <em>value</em> of <code>now</code> (which at this point has not been initialized) and converts it to a <code>const long*</code>; in other words, it takes a garbage numeric value and converts it to a pointer. It then attempts to take the address of that pointer -- but since it's a pointer <em>value</em>, not a pointer <em>object</em>, it has no address. The above is a compile-time error. (I'd expect your compiler to treat this as a fatal error, not just a warning.)</p> <p>But forget all that. Just declare your objects to be of the correct types in the first place, and you won't have to worry about casts.</p> <p>(There are cases where casts, even pointer casts, are necessary and appropriate, but not for what you're trying to do here.)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload