Solved

why we call *  the dereferencing operator in C programming?

Posted on 2007-11-29
16
828 Views
Last Modified: 2008-02-01
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
Comment
Question by:suoju1
  • 6
  • 5
  • 3
  • +1
16 Comments
 
LVL 53

Expert Comment

by:Infinity08
ID: 20375007
   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
 
LVL 40

Expert Comment

by:evilrix
ID: 20375428
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
 
LVL 53

Expert Comment

by:Infinity08
ID: 20376079
>> 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
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
LVL 40

Expert Comment

by:evilrix
ID: 20376129
>> 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
 

Author Comment

by:suoju1
ID: 20387303
*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
 
LVL 53

Expert Comment

by:Infinity08
ID: 20389163
>> i think all the above can help a little bit.

Which part are you still confused about ?
0
 

Author Comment

by:suoju1
ID: 20390558
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
 

Author Comment

by:suoju1
ID: 20390567
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
 
LVL 53

Accepted Solution

by:
Infinity08 earned 20 total points
ID: 20390813
>> 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
 
LVL 20

Expert Comment

by:ikework
ID: 20390825
> 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
 

Author Comment

by:suoju1
ID: 20391319
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
 
LVL 53

Expert Comment

by:Infinity08
ID: 20391343
>> 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
 

Author Comment

by:suoju1
ID: 20391614
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
 
LVL 53

Expert Comment

by:Infinity08
ID: 20391679
>> 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
 
LVL 20

Expert Comment

by:ikework
ID: 20391932
> 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
 
LVL 40

Expert Comment

by:evilrix
ID: 20392313
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question