Solved

why we call *  the dereferencing operator in C programming?

Posted on 2007-11-29
16
825 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
Comment Utility
   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
Comment Utility
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
Comment Utility
>> 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
 
LVL 40

Expert Comment

by:evilrix
Comment Utility
>> 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
Comment Utility
*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
Comment Utility
>> i think all the above can help a little bit.

Which part are you still confused about ?
0
 

Author Comment

by:suoju1
Comment Utility
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
Comment Utility
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 53

Accepted Solution

by:
Infinity08 earned 20 total points
Comment Utility
>> 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
Comment Utility
> 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
Comment Utility
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
Comment Utility
>> 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
Comment Utility
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
Comment Utility
>> 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
Comment Utility
> 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
Comment Utility
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files 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.

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now