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;
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;
&
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
.