• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 878
  • Last Modified:

why we call * the dereferencing operator in C programming?

why we call *  the dereferencing operator in C programming from the following example?

int y = 5;
int *yPtr;
yPtr = &y;    

*yptr returns y

so *  is the (dereferencing operator)

who is reference who??yPtr reference to y right?
0
suoju1
Asked:
suoju1
  • 6
  • 5
  • 3
  • +1
1 Solution
 
Infinity08Commented:
   int y = 5;
    int *yPtr = &y;

Now yPtr points to y, or "it references y".

When you do this :

    *yPtr

you follow the reference to get the value it refers to - you basically "get rid of" the reference, or "dereference".

Since yPtr points to y which has the value 5, *yPtr will evaluate to 5.
0
 
evilrixSenior Software Engineer (Avast)Commented:
It's a bit of a badly named operator as it doesn't dereference it de-points :) In C++ a reference is a completely different type with very different semantics. It's simpler to think of the * operator as the "get what it points to" operator -- no quite as catchy but semantically more correct :)
0
 
Infinity08Commented:
>> It's a bit of a badly named operator as it doesn't dereference it de-points :)

It is a term from the C era, and in C, the terms pointer and reference can be used interchangeably.

I like your "get what it points to" operator ;)
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
evilrixSenior Software Engineer (Avast)Commented:
>> It is a term from the C era, and in C, the terms pointer and reference can be used interchangeably.
Yes, I am aware of that but it is still semantically wrong -- more so when applied to C++ :)

>> I like your "get what it points to" operator
Catchy eh? :)

Here's the implementation: -

template <typename T>
T const & operator get_what_it_points_to(T const * t)
{
     return *t;
}
0
 
suoju1Author Commented:
*yPtr

you follow the reference to get the value it refers to - you basically "get rid of" the reference, or "dereference".


 It's simpler to think of the * operator as the "get what it points to" operator

i think all the above can help a little bit.

0
 
Infinity08Commented:
>> i think all the above can help a little bit.

Which part are you still confused about ?
0
 
suoju1Author Commented:
int value = 5;
int *ptr_to_value = &value;--->is a reference

*ptr_to_value = 6-->is a dereference?? why we call it a dereference??this way ptr_to_value still refer to address of value.

actually i understand all of your talking, i just curious what this term dereference really meaning.

thanks
0
 
suoju1Author Commented:
Infinity08:
   int y = 5;
    int *yPtr = &y;

Now yPtr points to y, or "it references y".

When you do this :

    *yPtr

you follow the reference to get the value it refers to - you basically "get rid of" the reference, or "dereference".

Since yPtr points to y which has the value 5, *yPtr will evaluate to 5



i don't think  *yPtr  "get rid of" the reference, or "dereference", as yPtr still refer the address of y.

thanks
0
 
Infinity08Commented:
>> i don't think  *yPtr  "get rid of" the reference, or "dereference", as yPtr still refer the address of y.

yPtr indeed still contains the address of y, and thus refers to y.

But, this line :

        *yPtr

is not JUST yPtr ... the dereference operator * is used to get the value at the address stored in yPtr ... in this case, to get the current value of y. We dereference the yPtr reference to get the actual value of what the reference refers to.

So :

        yPtr

is a reference TO some value, while :

        *yPtr

will get the actual value ... *yPtr will evaluate to the value that the reference points to - we followed the reference, and now have the value itself.



Let's look at it differently :

        int y = 5;

In memory, we have now reserved enough space for an int value 5 :

        --------------
        |    y (5)    |
        --------------

Now we create a pointer that will point to that value 5 :

        int *yPtr = &y;

In memory, we have something like this :

        -------------           --------------
        |    yPtr    | -----> |    y (5)    |
        -------------           --------------

yPtr now refers to y (points to y).

Now, when we do this :

        *yPtr

we follow the pointer to get the value, so yPtr will evaluate to the int value 5.
0
 
ikeworkCommented:
> i don't think  *yPtr  "get rid of" the reference, or "dereference", as yPtr still refer the address of y.

both, the combination of the prefix "de-" in the word dereference and the "get rid of"-phrase dont mean, that the pointer gets unusable, corrupted or changed.
0
 
suoju1Author Commented:
i agree with both of you, but i am still have probelm with the word "dereference", what is it really mean?
oringinally used and used later on here?

thanks lot
0
 
Infinity08Commented:
>> i agree with both of you, but i am still have probelm with the word "dereference", what is it really mean?

It means what I've been explaining all along ... you follow the reference to get the value ... you de-reference. It's just a term.

Which part of my explanation do you have a problem with ?
0
 
suoju1Author Commented:
ikework:
its just the terminology, this word describes that action. wiki says this at http://en.wikipedia.org/wiki/Reference_(computer_science)

"Accessing the value referred to by a reference is called dereferencing"

the term "dereference" means something like to retrace, to backtrack, to trace back, like follow the way back to the origin/source. but i better leave that to native speakers. i guess they can explain the word better than me. thats just what my dictionary says .. ;)

ike
would you let me know the place where you get the dictionary explain about the word dereference.

thanks lot
0
 
Infinity08Commented:
>> would you let me know the place where you get the dictionary explain about the word dereference.

What do you mean ? I've already explained what it means ... If you want to look it up in a dictionary, then look under the d obviously. But it will hardly be better than what has already been said here.

Can you tell me what still confuses you ?
0
 
ikeworkCommented:
> would you let me know the place where you get the dictionary explain about the word dereference.

well as infinity said, you'll hardly find more information there, since everything was said here already. the dictionary i use, however is http://dict.tu-chemnitz.de/ its for spanisch, german and english ..

0
 
evilrixSenior Software Engineer (Avast)Commented:
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 6
  • 5
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now