References vs Pointers?

Discussion in 'C' started by RobotGymnast, Aug 1, 2008.

  1. RobotGymnast

    RobotGymnast New Member

    Joined:
    Aug 1, 2008
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    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
     
  2. asadullah.ansari

    asadullah.ansari TechCake

    Joined:
    Jan 9, 2008
    Messages:
    356
    Likes Received:
    14
    Trophy Points:
    0
    Occupation:
    Developer
    Location:
    NOIDA
    You can get anywhere...Just Google it...
     
  3. zamjad

    zamjad New Member

    Joined:
    Oct 7, 2008
    Messages:
    15
    Likes Received:
    1
    Trophy Points:
    0
    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.
     
  4. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
  5. asadullah.ansari

    asadullah.ansari TechCake

    Joined:
    Jan 9, 2008
    Messages:
    356
    Likes Received:
    14
    Trophy Points:
    0
    Occupation:
    Developer
    Location:
    NOIDA
    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.
     
  6. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    > 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.
     
  7. zamjad

    zamjad New Member

    Joined:
    Oct 7, 2008
    Messages:
    15
    Likes Received:
    1
    Trophy Points:
    0
    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.
     
  8. zamjad

    zamjad New Member

    Joined:
    Oct 7, 2008
    Messages:
    15
    Likes Received:
    1
    Trophy Points:
    0
    got your point. But from your this reply

    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.
     
  9. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    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.
     
  10. zamjad

    zamjad New Member

    Joined:
    Oct 7, 2008
    Messages:
    15
    Likes Received:
    1
    Trophy Points:
    0
    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/wg21/docs/papers/2008/n2800.pdf
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice