Solved

why we call *  the dereferencing operator in C programming?

Posted on 2007-11-29
16
832 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

740 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