帮看下这个小程序,有点懵

昵称很不好取 2010-03-09 01:00:02
这样写两个结果一样
#include <iostream>
#include <ctime>
using namespace std;

int main()
{
time_t t = time(NULL);
struct tm * p1 = gmtime(&t);
struct tm * p2 = localtime(&t);

cout<<p1->tm_hour<<endl;
cout<<p2->tm_hour<<endl;

system("pause");
return 0;
}

这样写就正确了,时间相差8小时
#include <iostream>
#include <ctime>
using namespace std;

int main()
{
time_t t = time(NULL);
struct tm * p1 = gmtime(&t);
cout<<p1->tm_hour<<endl;

struct tm * p2 = localtime(&t);
cout<<p2->tm_hour<<endl;

system("pause");
return 0;
}
...全文
131 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
Paradin 2010-03-09
  • 打赏
  • 举报
回复
引用 8 楼 kevinz26 的回复:
MSDN:

The gmtime, mktime, and localtime functions use the same single, statically allocated structure to hold their results. Each call to one of these functions destroys the result of any previous call.

只有引用的内容不允许回复!
kevinz26 2010-03-09
  • 打赏
  • 举报
回复
MSDN:

The gmtime, mktime, and localtime functions use the same single, statically allocated structure to hold their results. Each call to one of these functions destroys the result of any previous call.
rularys 2010-03-09
  • 打赏
  • 举报
回复
引用 4 楼 kktemp234 的回复:
p1和p2指向的是同一个位置。

gmtime和localtime返回的都是static变量


如果如4楼所说,那么第一个程序的两个输出应该和第二个的p2输出一致。难道真是这样么?
fengzi0503 2010-03-09
  • 打赏
  • 举报
回复
两个引用指向的是同一个地址
pengzhixi 2010-03-09
  • 打赏
  • 举报
回复
引用 3 楼 thefirstz 的回复:
引用 2 楼 abcix 的回复:不明白LZ要说的是什么意思
~~~~~~~~~~~~~~~


LZ啊你第一个程序的t struct tm * p2 = localtime(&t); 最后是以这个t为答案啊
最帅马老师 2010-03-09
  • 打赏
  • 举报
回复
p1和p2指向的是同一个位置。

gmtime和localtime返回的都是static变量
昵称很不好取 2010-03-09
  • 打赏
  • 举报
回复
引用 2 楼 abcix 的回复:
不明白LZ要说的是什么意思

~~~~~~~~~~~~~~~
Abcix 2010-03-09
  • 打赏
  • 举报
回复
不明白LZ要说的是什么意思
rularys 2010-03-09
  • 打赏
  • 举报
回复
懵了~~~~~~~~

64,639

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧