如何访问的timeval结构的字段 [英] How to access the fields of a timeval structure
本文介绍了如何访问的timeval结构的字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在来打印值timeval结构
变量如下:
int main()
{
struct timeval *cur;
do_gettimeofday(cur);
printf("Here is the time of day: %ld %ld", cur.tv_sec, cur.tv_usec);
return 0;
}
我不断收到此错误:
I keep getting this error:
request for member 'tv_sec' in something not a structure or union.
request for member 'tv_usec' in something not a structure or union.
我该如何解决这个问题?
How can I fix this?
推荐答案
由于 CUR
是一个指针。使用
struct timeval cur;
do_gettimeofday(&cur);
在Linux中, do_gettimeofday()
要求用户pre-分配的空间。不只是传递一个没有指向任何一个指针!你可以使用的malloc()
,但你最好的选择仅仅是在栈上传递的东西的地址。
In Linux, do_gettimeofday()
requires that the user pre-allocate the space. Do NOT just pass a pointer that is not pointing to anything! You could use malloc()
, but your best bet is just to pass the address of something on the stack.
这篇关于如何访问的timeval结构的字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文