0
1. struct thread_args 
   {
2.   int thread_id;
3.   struct timeval before;
4.   struct timeval after;
   }

5. void * foobar(void *threadargs)
   {
6.   struct * mydata;
7.   mydata = (struct thread_args *) threadargs;

8.   gettimeofday(&mydata->before, NULL);
9.   gettimeofday(mydata->before, NULL);
   }

...

What's the difference between lines 8 and 9? The former compiles, the latter does not, but even though the first one compiles I'm not sure that it does what I want.

Jefferson Hudson
  • 710
  • 1
  • 10
  • 22
  • [`gettimeofday`](http://linux.die.net/man/2/gettimeofday) requires a `struct timeval *` as the first argument, does that give you a clue what the difference is? – Praetorian Mar 06 '14 at 19:48
  • I'm under the impression that the & returns the address of the variable to which the pointer is pointing. I don't see how you can dereference an address. – Jefferson Hudson Mar 06 '14 at 19:50
  • Applying the address of operator (`&`) to a type `T` will return a value of type `T*`, which contains the address of the object that you applied `&` to. This value is typically used to initialize/assign a pointer variable of the same type. You're allowed to dereference this value as long as it points to a valid object. – Praetorian Mar 06 '14 at 20:01
  • is it possible to dereference anything else? – Red Alert Mar 06 '14 at 20:02
  • every lvalue http://stackoverflow.com/questions/3601602/what-are-rvalues-lvalues-xvalues-glvalues-and-prvalues – Adrian Krupa Mar 06 '14 at 20:11

1 Answers1

3

foo->bar = value contained by variable bar in struct variable foo.

&foo->bar = address of variable bar in struct variable foo.

See this operator precedence table.

gettimeofday() requires first argument of type struct timeval *. So gettimeofday(mydata->before, NULL); does not compile.

Rikayan Bandyopadhyay
  • 4,927
  • 2
  • 18
  • 32