time 함수에 관한 질문입니다.
agine
2023.04.01
printf(%s\n, asctime(curr_tm));
printf(%s\n, asctime(bir_tm));
에서 둘다 계속 같은 값이 나옵니다.
각각 인자를 다르게 받아서 값을 수정하여도 그렇게 됩니다.
왜 그렇습니까?
#include time.h
#includestdio.h
#include conio.h
#include stdlib.h
#include Windows.h
void main()
{
time_t curr, bir;
struct tm *curr_tm, *bir_tm;
curr = time(NULL);
bir = time(NULL);
curr_tm = localtime(&curr);
bir_tm = localtime(&bir);bir_tm-tm_year = 83;
bir_tm-tm_mon = 11;
bir_tm-tm_mday = 26;
printf(%s\n, asctime(curr_tm));
printf(%s\n, asctime(bir_tm));
}
-
SweetChoco
네 localtime이 리턴하는 변수의 가 static형 입니다. 따라서, 첫 번째 호출하고 나서 두 번째 호출하면 첫 번의 데이터가 파괴되고 두 번째 호출된 자료로 바꿔지게 됩니다.
따라서 정상적으로 사용하려면 리턴된 것을 복사해서 써야합니다. -
빈길
localtime()이 같은곳 주소만 리턴해서 두 구조체 포인터가 한곳을 같이 가리키고 있네요..
원래 그런걸까요? -_-;