Why does time(time_t *) function both return and set the by-ref?

前端 未结 2 482
傲寒
傲寒 2020-12-03 13:40

I\'ve always been curious, why does the time(time_t *) function both return a time_t, and set the time to the passed in pointer?

Example of

2条回答
  •  独厮守ぢ
    2020-12-03 14:22

    It allows you to nest a call to time() within another expression, instead of doing it in a separate statement:

    time_t x = time(&now) + more_time;
    

    When the above statement finishes, now should contain the current time, and x should contain the current time plus some value.

    strcpy falls in the same case because it returns the same char * pointer that has been passed as its destination, so nesting it is possible as well:

    printf("Copied string is %s", strcpy(dst, src));
    

提交回复
热议问题