Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   References vs Pointers? (http://www.go4expert.com/forums/references-vs-pointers-t12636/)

RobotGymnast 2Aug2008 01:25

References vs Pointers?
 
I know somebody posted a thread about this, but that was really only listing in what ways pointers are better than references, but it didn't compare really. It also didn't give situations where either would be useful, when to use what, pros and cons, etc. What are the pros and cons? When should I use which?

Thanks, RobotGymnast

asadullah.ansari 9Feb2009 14:12

Re: References vs Pointers?
 
You can get anywhere...Just Google it...

zamjad 10Feb2009 02:31

Re: References vs Pointers?
 
Quote:

Originally Posted by asadullah.ansari (Post 42660)
You can get anywhere...Just Google it...

Its interesting that patron of the year gave this reply. Just imagine if we are suppose to get the similar answers from our teachers :)

Well back to the topic.

Here are the basic differences

1. Reference has to assign to something at the time of decleration, however pointer dont have to.
2. Reference can't be null, but pointer can be so no need to check for null reference just like we do in case of pointers
3. Once reference is assigned it can't be changed to assigned something else. On the other hand pointer can point to different thing after reassingment.

If there is still confusion please feel free to post it.

PS. there are no dumb or stupid questions.

shabbir 10Feb2009 09:20

Re: References vs Pointers?
 
Also you can Check out - Difference between Pointers and Reference in C++

asadullah.ansari 10Feb2009 10:17

Re: References vs Pointers?
 
Quote:

Originally Posted by zamjad (Post 42701)
Its interesting that patron of the year gave this reply. Just imagine if we are suppose to get the similar answers from our teachers :)

Well back to the topic.

Here are the basic differences

1. Reference has to assign to something at the time of decleration, however pointer dont have to.
2. Reference can't be null, but pointer can be so no need to check for null reference just like we do in case of pointers
3. Once reference is assigned it can't be changed to assigned something else. On the other hand pointer can point to different thing after reassingment.

If there is still confusion please feel free to post it.

PS. there are no dumb or stupid questions.

First thing i didn't mean that whatever you mention. Definition of any term(reference & pointer) offcourse you can find anywhere, any basic books...So what i expect that the guy who asked this question, will search and read about it and come up with some doubts, some problem based on it etc, so it's benefit to him only...
To develope a person knowledge go and just read any books based on " How to make teaching style efficient" so that it will boost your learning capacity, thinking capacity etc.

xpi0t0s 10Feb2009 15:01

Re: References vs Pointers?
 
> Its interesting that patron of the year gave this reply. Just imagine if we are suppose to get the similar answers from our teachers

Asadullah's correct, it's a waste of time typing a response to a question that can easily be Googled. The difference between us and your teachers is that they are paid to teach you directly. If you want to pay us the appropriate rate then yes of course we'll teach you directly instead of pointing you to Google.

But while we're giving you our help free of charge you take it on our terms, which are that you Google stuff first and check out the basics, THEN we'll help you with stuff you don't understand.

Also, an essential skill for programmers is that you can find out stuff for yourself. In the course of a programmer's life you will read MANY manuals and you will have to dig, often for hours on end, to find stuff out, and there won't always be someone on hand to spoon-feed you the answer. If you can't find something then there are two problems (1) you need to find something out and (2) you don't know where to look. If you haven't even bothered to check Google then you're going to get a short answer - JFGI.

And it's obvious you haven't checked Google when the question you asked is entered into Google verbatim and the first few hits contain the answer. For example just Google "c++ pointer reference" - the top hit doesn't seem relevant to me but hits 2,3,4,5 and 6 seem directly relevant.

And in fact the TOP hit in Google for "References vs Pointers" is directly relevant.

zamjad 10Feb2009 18:32

Re: References vs Pointers?
 
I dont mind for whatever your terms and condition are for telling the answer is, but just want to clear at least one thing.

Quote:

Originally Posted by xpi0t0s (Post 42722)

And it's obvious you haven't checked Google when the question you asked is entered into Google verbatim and the first few hits contain the answer. For example just Google "c++ pointer reference" - the top hit doesn't seem relevant to me but hits 2,3,4,5 and 6 seem directly relevant.

It was not me who asked this question, so whatever you wanna say; tell it directly to the OP. Instead of "you" it should be OP.

zamjad 10Feb2009 18:41

Re: References vs Pointers?
 
Quote:

Originally Posted by asadullah.ansari (Post 42713)
First thing i didn't mean that whatever you mention. Definition of any term(reference & pointer) offcourse you can find anywhere, any basic books...So what i expect that the guy who asked this question, will search and read about it and come up with some doubts, some problem based on it etc, so it's benefit to him only...
To develope a person knowledge go and just read any books based on " How to make teaching style efficient" so that it will boost your learning capacity, thinking capacity etc.

got your point. But from your this reply

Quote:

Originally Posted by asadullah.ansari (Post 42713)
You can get anywhere...Just Google it...

It look very rude to me and probably the OP too. In the forum, we donít see the face of other, so whatever we judge about others personalities are from their words. You can explain the same thing in little bit polite way just like you did aftermath. I appreciate your detailed reply.

xpi0t0s 10Feb2009 22:25

Re: References vs Pointers?
 
Quote:

Originally Posted by zamjad (Post 42730)
I dont mind for whatever your terms and condition are for telling the answer is, but just want to clear at least one thing. It was not me who asked this question, so whatever you wanna say; tell it directly to the OP. Instead of "you" it should be OP.

Sorry, that's a nuance of the English language (which is my first). I said "when", not "because", so the statement does not imply you asked the question, which I knew when I made that comment.

zamjad 11Feb2009 00:15

Re: References vs Pointers?
 
Now this question is little bit outdates. In new version of C++ also known as C++0x there are two types of references. One is lvalue reference (same as old one) and the new one is rvalue reference.

The rvalue reference is decleared and initilized as

Code:

int iValue = 10;
int&& ref = iValue

Note there are two & signs with rvalue reference not one.

For further information you can read it from the draft of C++ standard document no n2800You can get this document here

http://www.open-std.org/jtc1/sc22/wg...2008/n2800.pdf


All times are GMT +5.5. The time now is 11:16.