gettimeodday()函数问题?
源函数:
#include <time.h>
#include <stdio.h>
#include <sys/time.h>
int main( void)
{
struct tm *local;
struct timeval *tv1,*tv2;//这样不对吗?
time_t t;
t=time(NULL);
local=localtime(&t);
printf("local hour is: %d \n",local->tm_hour);
local=gmtime(&t);
printf("utc hour is: %d \n",local->tm_hour);
printf("time is %s \n",asctime (local));
printf("time is %s \n",ctime(&t));
gettimeofday(tv1,NULL);
printf("interval time is %ld \n",tv1->tv_sec);
sleep(10);
gettimeofday(tv2,NULL);
printf("interval time is %ld \n",tv2->tv_sec);
return 0;
}
第一次运行:
local hour is: 9
utc hour is: 1
time is Fri Jun 8 01:20:53 2012
time is Fri Jun 8 09:20:53 2012
interval time is 430687105
interval time is 1547644
第二次运行:
local hour is: 9
utc hour is: 1
time is Fri Jun 8 01:20:53 2012
time is Fri Jun 8 09:20:53 2012
interval time is 430687105
interval time is 1547644
为什么,两次运行的结果一样呢?
并且这个结果好像不对把?
我把
struct timeval *tv1,*tv2;改为struct timeval tv1,tv2;
后
运行
#include <time.h>
#include <stdio.h>
#include <sys/time.h>
int main( void)
{
struct tm *local;
struct timeval tv1,tv2;
time_t t;
t=time(NULL);
local=localtime(&t);
printf("local hour is: %d \n",local->tm_hour);
local=gmtime(&t);
printf("utc hour is: %d \n",local->tm_hour);
printf("time is %s \n",asctime (local));
printf("time is %s \n",ctime(&t));
gettimeofday(&tv1,NULL);
printf("interval time is %ld \n",tv1.tv_sec);
sleep(10);
gettimeofday(&tv2,NULL);
printf("interval time is %ld \n",tv2.tv_sec);
return 0;
}
运行:
local hour is: 9
utc hour is: 1
time is Fri Jun 8 01:39:43 2012
time is Fri Jun 8 09:39:43 2012
interval time is 1339119583
interval time is 1339119593
这样貌似正确了,这是为什么啊??
谢谢大家!!
------解决方案--------------------前面的程序是错的,后面的是正确的
------解决方案--------------------没内存啊,哥.
C语言和没学过一样,说实话.
------解决方案--------------------第一程序只给了指针地址,没有内存确保,就是两个野指针啊。
------解决方案--------------------前者,野指针,需要先分配内存