tags:

views:

92

answers:

5

I'm trying to print the values in a struct timeval variable as follows:

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?

+3  A: 

You need to use the -> operator rather than then . operator when accessing the fields. Like so: cur->tv_sec.

Also you need to have the timeval structure allocated. At the moment you are passing a random pointer to the function gettimeofday().

struct timeval cur;
gettimeofday(&cur);
printf("%ld.%ld", cur.tv_sec, cur.tv_nsec);
Marc
If cur is on the stack, then it is not a pointer. Can't use -> operator then.
chrisaycock
Ok I just tried that, I got any error saying dereferencing pointer to incomplete type. Any ideas?
Gabe
@chrisaycock: Nonsense. In the original code, `cur` certainly is a pointer, and certainly is on the stack (i.e., has `auto` storage class).
Jerry Coffin
@Gabe Don't use the arrow (->), just stick to the dot (.) instead.
chrisaycock
@Gabe Mixed up my example code, in which cur is no longer a pointer. Sorry about that.
Marc
+6  A: 

Because cur is a pointer. Use

struct timeval cur;
do_gettimeofday(&cur);

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.

chrisaycock
+2  A: 

The variable cur is a pointer of type timeval. You need to have a timeval variable and pass it's address to the function. Something like:

struct timeval cur;
do_gettimeofday(&cur);

You also need

#include<linux/time.h>

which has the definition of the struct timeval and declaration of the function do_gettimeofday.

Alternatively you can use the gettimeofday function from sys/time.h.

Working link

codaddict
That function is in `<linux/time.h>`, and the error is because he's trying to access a member of a pointer.
Matthew Flaschen
@Matt, thanks for the error part.
codaddict
The answer is still misleading about `do_gettimeofday`. As I said, it's a function declared in `<linux/time.h>`, and that header also defines `struct timeval`.
Matthew Flaschen
If you can take a look at my next post, I would be most appreciative.
Gabe
+1  A: 

Ok so I tried changing the dot operator to the this operator (->).
I made the change to the declaring the variable to struct timeval cur, not a pointer; and
changed the subsequent call to do_gettimeofday(&cur). Now I have the issue of:
error: storage size of 'cur' isn't known.

Thoughts?

Gabe
Can you paste the full code? And just to make sure, you aren't using the operator -> now that cur isn't a pointer, right?
chrisaycock
That is correct I am using the dot operator to access
Gabe
A: 

Ok, so I'm running out of linux kernel version 2.6.32, in which I don't believe there is a sys/time.h header, the linux/time.h header contains the definition of the struct timeval
http://lxr.linux.no/#linux+v2.6.32/include/linux/time.h (line 20)

My current version of this main looks like this:

#include <time.h>    
#include stdio.h    
#include unistd.h    

int main()    
{    

struct timeval cur;    
do_gettimeofday(&cur);    
printf("Here's the time of day: %ld.%ld", cur.tv_sec, cur.tv_usec);    

return 0;    
}    

This brings me back to the error of:
storage size of 'cur' is not known

Maybe this will clear something up?

Gabe
And that is all the code in this main function.
Gabe
Which line number is the compiler complaining about?
chrisaycock
line 8 where the timeval is initially declared
Gabe