What is the difference between `*a =` and `= *a`?

261 views Asked by At

In following function,

void swap(int * a, int * b) {
    int t;
     t = *a; // = *a
    *a = *b; // a* =
    *b =  t;
}

What is the difference between = *a and *a =?

I've heard that the * operator in = *a is a de-referencing(or in-directing) operator which fetches(?) that value from the pointer.

Then, what is the actual meaning of *a =?


Yesterday, the day I asked this question, I explained about pointers to my colleague whose major field has nothing to do with pointers.

I quickly typed a source code like this.

#include <stdio.h>

void swap1(int a , int b) {
    int t = a;
    a = b;
    b = t;
}

void swap2(int * a, int * b) {
    int t = *a;
    *a = *b;
    *b = t;
}

int main(int argc, char * argv[]) {
    int a = 10;
    int b = 20;
    swap1(a, b);
    swap2(&a, &b);
}

I was even proud of myself for remembering things imprinted on my brain in 1996. (I've been with Java for almost 20 years.) I used a bunch of printfs with %ds and %ps to show her what was happening.

Then I made a terrible mistake. I declared.

포인터 변수 앞에 을 붙이면 값을 가져온다는 뜻이에요.

When you attach a STAR in front of a pointer variable, that means you fetches(retrieves) the value.

Well that could be applied to following statement.

int t = *a;

Which simply can be said,

int t = 10;

The big problem I faced came from the second statement.

*a = *b; // 10 = 20?

The root evil is that I didn't try to explain about the dereference operator or I didn't ask to myself of being aware of the meaning of a 별(star).

Here is what people would say.

for = *a,

the actual value at the address denoted by a is assigned to the left side.

for *a =

the value of the right side is stored in the address denoted by a.

That's what I'm confusing of. And that's why I should re-think about the meaning of `de-referencing'.

Thanks for answers.


Oh I think this problem is going deeper to the concepts of lvalues and rvalues.

6

There are 6 answers

0
Spikatrix On BEST ANSWER

Here:

t = *a;

the pointer a is dereferenced and this value is assigned to t whereas here:

*a = *b;

both b and a are dereferenced and the value of *b is stored in the address a.

0
Sourav Kanta On

It has no relation with '=' operator .It(* operator) just means value at 'a's address.So

t = *a;   assigns value at address a to t
*a = *b;  assigns b's value in place of value at address a
*b =  t;  assigns t to b's value 
0
Seema Kadavan On

a is a pointer here, pointing to an int.

*a retrieves the integer value stored at memory pointed to by a. When you say *a = <some value>, assuming <some value> to be an int, <some value> gets stored at memory location pointed to by a.

*a = *b ==> Here b is again an int pointer, so integer value pointed to by b is read and written to memory location pointed to by a.

0
Sunil Bojanapally On

I've heard that the * operator in = *a is a de-referencing(or in-directing) operator which fetches(?)

In fact fetch happens when you dereference a pointer with operator * alone. Hence assignment = operator doesn't involve in dereferencing perhaps assigning the dereferenced value to LHS.

0
undur_gongor On

The first one is reading from the memory a is pointing to. The second one is writing to that memory.

This is no different from x = and = x except that you do not access the variable directly but the object it is pointing to.

0
Lundin On

What is the difference between = *a and *a =?

Well... look at this code:

int x;
t = x;
x = t;

If you have any plain variable int x; then you can read from it and write to it. That's the difference between your two lines as well.

* is the "contents of" operator. If a is a pointer to int, then *a simply means that you can use the contents of that variable as if it had been a plain int variable. And just like any variable you can either read or write to it.