ne***********@gmail.com wrote:
Hi, in this statement:
int num1, *ptr;
num1=50;
ptr=&num1;
*ptr = (*ptr+25);
The indirection operator in (*ptr+25) says take the value that is held
in the address pointed to *ptr (which is the address of num1 the value
being 50).
So what does the *ptr on the left side of the assignment do? Is it
saying take the result of the right side (75) and assign it to the
location in memory pointed to by *ptr (or &num1)?
Yes. The * is necessary, because on the left side you always implicitely
specify the address of where to store the right hand side
You dont write:
int i;
&i = 5;
meaning: store 5 at *the address of* i
you write
i = 5;
and the compiler knows that it has to take the address of the target
in contrast to j = i; where it has to take the value of i.
Consequently you also write
*ptr = ....
(ptr already holds an address, * gives you the corresponding object)
if you want to store something at the memory location where ptr points to.
--
Karl Heinz Buchegger
kb******@gascad.at