Unix Reference Value Call

While I am debugging my small porting program, I have a question about the differences between types.

Here is my source (below):

  1 #include <time.h>
  2 #include <stdio.h>
  3 
  7 int main () {
  8     clock_t now; //unsigned long 
  9     struct tm * mac_time; // const long
 10     char *time_str;
 11     now = time (NULL);
 12     mac_time = localtime((const long *)&now); 
 13                           // localtime ( const long <- unsigned long)
 13     time_str = asctime(mac_time);
 14     printf("Now : %s\n",time_str);
 15     return 0;
 16 }

Firstly, it works correctly, there is no error right now. But I have a question about the differences in casting types.

On line 12, since there was a warning about type discrimination, I changed the value type for debugging.

but what is the difference between

12     mac_time = localtime((const long *)&now);

and

12     mac_time = localtime(&(const long *)now);

I thought that there is no difference between each other, because it differs only from "casted value address" and "value address casted".

But the compiler issues a warning to the latter.

Will you give me some tips on this issue?

+4
source share
3 answers

mac_time = localtime((const long *)&now);  

&now, pointer to clock_t, const long *, .. const long. , localtime pointer to const long.

 mac_time = localtime(&(const long *)now);  

now clock_t, const long *, , .
:

error: cannot take the address of an rvalue of type 'const long *' mac_time = localtime(&(const long *)now); 

, r-value. & l- . &(const long *)now, .

, , , C, .

+4

(const long *)&now now const long.

&(const long *)now now const long . , float - , , now, .

+5

. , . ( "", , .)

localtime() const time_t*, clock_t*. time_t clock_t - , , .

now :

time_t now;

localtime :

mac_time = localtime(&now);

, , . , , , .

, - , , . :

mac_time = localtime((const long *)&now)

now ( clock_t*) const long*. localtime const time_t*, , -, time_t long , .

:

mac_time = localtime(&(const long *)now);

now ( ) const long*; , . , , -, . . ( , , .)

. , .

( , , , , , .)

+4

All Articles