-7

I am new to C programming, and I am trying to figure out what does the & do in (&end) in this timer but I cannot find a sufficient answer anywhere.

time_t end, current;
time(&end);
end += t;
Tas
  • 7,023
  • 3
  • 36
  • 51
Dani Ene
  • 23
  • 1
  • 7
  • `&` is used to pass address of `end` variable to the `time` function. – haccks Jan 28 '16 at 03:52
  • Why the C++ tag if you're asking about the C language? They are different. For example, the `&` symbol can be used in the C++ language to declare *references*, which C doesn't have. – Thomas Matthews Jan 28 '16 at 04:07
  • I was about to downvote, thinking there'd be plenty of resources about this question; however, I was unable to find anything specific to C and the `&` in a quick search. – Tas Jan 28 '16 at 04:11
  • Possible duplicate of [Pointers in C: when to use the ampersand and the asterisk?](http://stackoverflow.com/questions/2094666/pointers-in-c-when-to-use-the-ampersand-and-the-asterisk) –  Jan 28 '16 at 05:25

1 Answers1

2

& is the address-of operator. It is a unary operator with returns the address of its operand in memory (a pointer to it).

In this case, the function time is defined as

time_t time(time_t *t)

As you can see, time accepts a pointer to a time_t type variable. This is referred to as passing a reference (not strictly accurate, as it's really just a pointer in c but still some useful terminology). It allows time_t passed in to be modified directly. Basically, instead of passing in end you are saying "get a pointer that points at end and pass that in". So by calling time(&end);, the time function can set the value of end even though it is being passed in as a function argument.

I have to say though, some quick googling would have certainly provided a complete explanation. For more info I'd suggest looking up c ampersand operator and c address-of operator, as well as pass by reference vs pass by value.

CollinD
  • 7,304
  • 2
  • 22
  • 45