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

advertisements

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 `printf`s with `%d`s and `%p`s 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 `lvalue`s and `rvalue`s.

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`.