After assigning a value to the local2 pointer, the previously assigned value of the local pointer local2 overwritten.
Please explain the nature of this rewrite.
The task:
Create a function that calculates the time interval between two time stamps as a number of hours, minutes and seconds. The time stamps of the function should be obtained through the parameters of the structure type, and the result should be returned as the value of the structure variable.
#include <stdio.h> #include <time.h> // описание функции struct tm timegap(struct tm * lc1, struct tm * lc2) { struct tm res; res = *lc2; res.tm_hour = lc1->tm_hour - lc2->tm_hour; res.tm_min = lc1->tm_min - lc2->tm_min; res.tm_sec = (lc1->tm_sec) - (lc2->tm_sec); return res; } void main () { // объявление переменных time_t crt_time; struct tm *local,*local2,result; // отметка времени getch(); crt_time = time(NULL); local = localtime(&crt_time); printf("%s\n",asctime(local)); // отметка времени №2 getch(); crt_time = time(NULL); local2 = localtime(&crt_time); //в этом месте наглядно виден факт перезаписи printf("%s\n",asctime(local2)); printf("%s\n",asctime(local)); // функции выводят одинаковые значения /* result = timegap(local2,local); printf("%s\n",asctime(&result)); */ }
localtime(&crt_time)withlocaltime(crt_time)? - Nikolaj Sarry