C 언어 포인터 전달
오늘 Liux 의 다음 C 언어 가 시스템 시간 을 가 져 오 는 코드 를 찾 았 습 니 다.
time_t now = time(0);
struct tm *tnow = localtime(&now);
printf("%d-%d-%d %d:%d:%d
",1900+tnow->tm_year,
tnow->tm_mon+1,
tnow->tm_mday,
tnow->tm_hour,
tnow->tm_min,
tnow->tm_sec);
먼저 두 번 째 줄 코드, gcc 컴 파일 러 는 tm * tnow 의 방식 으로 정의 할 수 없 으 며, struct 로 시작 해 야 합 니 다. 그렇지 않 으 면 알림 이 정의 되 지 않 습 니 다.g + + 는 가능 합 니 다.그리고 함수 로 포장 하고 싶 습 니 다. 나중에 사용 하기에 편리 합 니 다. 결 과 는:void vGetTime(struct tm *tnow)
{
time_t now = time(0);
tnow = localtime(&now);
printf("%d-%d-%d %d:%d:%d
",1900+tnow->tm_year,
tnow->tm_mon+1,
tnow->tm_mday,
tnow->tm_hour,
tnow->tm_min,
tnow->tm_sec);
}
// , struct tm now , vGetTime(&now) 。
실제로 함수 로 포장 한 후 tnow 포인터 가 바 뀌 었 습 니 다. 호출 된 now 로 돌아 갈 때 주 소 를 바 꾸 었 습 니 다.이 기초 의 잘못 은 거의 한 시간 동안 찾 았 다.나중에 다음 과 같이 수정 합 니 다.void vGetTime(struct tm *tnow)
{
time_t now;
struct tm *tmp;
now = time(0);
tmp = localtime(&now);
tmp->tm_year += 1900;
tmp->tm_mon += 1;
memcpy((void*)tnow,(const void *)tmp,sizeof(struct tm));
}
마지막 으로 포함 해 야 할 헤더 파일 은 stdio. h, time. h, string. h (string. h 는 memcpy 가 필요 합 니 다. 경 고 는 포함 되 지 않 지만 잘못 보고 하지 않 습 니 다)
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Docker를 사용한 React 및 .NET Core 6.0 샘플 프로젝트 - 1부이 기사에서는 Entity Framework Core Code First 접근 방식을 사용하는 ASP.NET Core 6.0 WEP API의 CRUD(만들기, 읽기, 업데이트 및 삭제) 작업에 대해 설명합니다. 웹 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.