0

For this program:

#include <stdio.h>
#include <time.h>
 
int main ()
{
    time_t seconds;
     
    seconds = time(NULL);
    printf("Hours since January 1, 1970 = %ld\n", seconds/3600);
     
    return(0);
}

The output is: Hours since January 1, 1970 = 460587

Now a slightly change in program:

#include <stdio.h>
#include <time.h>
 
int main ()
{
    time_t seconds;
     time_t *s;
    seconds = time(s);
    printf("Hours since January 1, 1970 = %ld\n", seconds/3600);
     
    return(0);
}

The output is:
Hours since January 1, 1970 = 460587
I want to know the difference while using NULL and *s

wohlstad
  • 12,661
  • 10
  • 26
  • 39
  • 1
    In the 2nd case `s` is an uninitialized pointer. The result should be UB. – wohlstad Jul 18 '22 at 03:33
  • 2
    If you write `time_t s; seconds = time(&s);`, then the value returned by `time` is also stored in `s`. It's in the documentation. But passing the uninitialized `s` to `time` as you've done is just undefined behavior. – William Pursell Jul 18 '22 at 03:36
  • Thanks, edited successfully Please tell me the difference in using `&s` and `NULL` – Anup Adhikari Jul 18 '22 at 04:06
  • 1
    Do NOT modify your question to invalidate answers that have already been given. You could amend the question to add the fixed code, but you may not remove erroneous code just because you can see how bad it is. – Jonathan Leffler Jul 18 '22 at 04:06
  • Ok... I shall delete it soon. Please answer to that one. – Anup Adhikari Jul 18 '22 at 04:09
  • 1
    If you use: `time_t t1; time_t t2 = time(&t1);` then `t1 == t2` after the call. You have two ways to get the answer back. Generally, passing a null pointer and simply assigning the result is sensible. There are historical reasons for the dual interface related to when 16-bit machines didn't necessarily have 32-bit integers (a _long_ time ago). Nowadays, you may as well assume that `NULL` is always appropriate as the argument. – Jonathan Leffler Jul 18 '22 at 04:09
  • Oh, it means when I pass a valid pointer I get my result stored in that pointer and variable both? Or – Anup Adhikari Jul 18 '22 at 04:11
  • @AnupAdhikari - yes this is what it means. – wohlstad Jul 18 '22 at 04:13
  • BTW @AnupAdhikari - I think you meant to change it to pass `&seconds`, **not** `&s` which is a pointer to a pointer (or `s = &seconds`, and then `time(s)`). – wohlstad Jul 18 '22 at 04:36
  • Welcome to StackOverflow. If an answer solves your problem you could click '✔' to mark it as an acceptable answer. You can also upvote any helpful answer (see here: https://stackoverflow.com/help/someone-answers). – wohlstad Jul 18 '22 at 04:59

1 Answers1

2

From the time function documentation the argument to the function should be:

pointer to a time_t object where the time will be stored, or a null pointer

In your 2nd case s is an uninitialized pointer which is neither.
The result is undefined behavior (UB). Once you get into UB anything can happen and you cannot rely on specific [possibly random] result.

You should either pass NULL, or a valid time_t pointer. In the later case the result will be updated in the object pointed by your pointer (and so it will be the same as the value returned by the function).

Proper usage examples:

Use the return value:

time_t t1 = time(NULL);

Or pass a pointer:

time_t t1;
time(&t1);

I can't think of a good reason for that, but you can also combine the two (in this case t1 will be equal to t2):

time_t t1;
time_t t2 = time(&t1);
wohlstad
  • 12,661
  • 10
  • 26
  • 39