Thread: Pointers v References

  1. #16
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Yes, this is possible:
    Code:
    const int& ref = 0;
    But ref only refers to the value 0. It has a different meaning than a NULL pointer which signifies that it doesn't point to anything.

    And anyway, you can only create your "null references" for integral types (or types where such implicit construction is possible) only.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  2. #17
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by rudyman View Post
    If null is defined as 0, and you can create a reference to 0, then you can create a reference to null. I somehow managed to say that in the most poorly worded way possible, but I was just pointing out that although technically there's no reason or use for it, it is possible to assign and check a reference to be null.
    While technically possible, it would rather assume that the object pointed to (referred to, referee), is rather 0 or does not have a reasonable value, but the reference itself is still valid, hence it isn't really a null reference.
    It is technically possible to emulate a null reference via C++ classes, though, if you really want a null reference.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #18
    Registered User
    Join Date
    Jul 2003
    Posts
    110
    Quote Originally Posted by m37h0d View Post
    I must admit that the nuance between these two escapes me.

    I understand the syntactical differences between using the two, but I do not understand how and when one is more useful than the other.
    I think another nuance to understand, is that references are not objects, but pointers are. I don't mean objects in an OOP sense, but rather in a storage sense. When you use the name of the pointer, you are referring to the pointer's place in memory, not the thing pointed to. For that, obviously you need the '*' to get to it. References are just a different name for something, and that is all. They are not a region of storage in their own right.

    I guess you could say that pointers are more useful when you want to use a "place" in memory, but references are better when you want to use a "thing" in memory. I hope that nuance is a clearer nuance than the nuance between pointers and references.

  4. #19
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You are incorrect in that references do not take storage. They do.
    I don't know what the standards say, but references are just a different way of using pointers. They work on the same concept, except with the syntax difference.
    When you pass something by reference, the address is passed of that object, just like a pointer. You just don't "see" it or do it manually. It's done automatically for you.

    Of course, references are NOT the same as pointers, but in the low level of things, they work the same [unless the standard states otherwise; I don't know what it says on this.]
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #20
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I think another nuance to understand, is that references are not objects, but pointers are. I don't mean objects in an OOP sense, but rather in a storage sense. When you use the name of the pointer, you are referring to the pointer's place in memory, not the thing pointed to. For that, obviously you need the '*' to get to it. References are just a different name for something, and that is all. They are not a region of storage in their own right.
    Conceptually true, though technically "it is unspecified whether or not a reference requires storage" (Paragraph 3, Section 8.3.2, 2003 edition of the C++ Standard).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #21
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    Generally, pointers are used for three prinicples.

    Arranging and allocating data on the heap
    Passing an argument to a function via reference
    Passing an object to a function

    Two of the above can be done via references, but obviously as many have pointed out, all memory is done via pointers ( new/delete).

    Pointers do have other uses of course, such as Nodes and link lists.
    Double Helix STL

  7. #22
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by swgh View Post
    Passing an argument to a function via reference
    I would like to point out that saying "by reference" is bad, since it's a (IMO) C++-only term and refers to references and not pointers, so the correct term should be passing an argument to a function via pointer.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #23
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    since it's a (IMO) C++-only term
    It's not. "Passing by reference" was used in C to describe passing a pointer before C++ even existed.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  9. #24
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I know it is, but I'm saying that expressing it that way is wrong and misleading. I urge the usage of pass by pointer instead to reduce confusion.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #25
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    swgh was pointing out that pointers can be used to implement (programming language-agnostic) pass-by-reference, as one learns about it in proper programming courses.

    It wouldn't make sense to say that pointers can be used to implement pass-by-pointer. That statement contains zero information.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  11. #26
    Registered User
    Join Date
    Jul 2003
    Posts
    110
    Quote Originally Posted by laserlight View Post
    Conceptually true, though technically "it is unspecified whether or not a reference requires storage" (Paragraph 3, Section 8.3.2, 2003 edition of the C++ Standard).
    Good point. Thanks for the clarification. I was primarily talking from a conceptual point of view, but I didn't make that explicit.

  12. #27
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by CornedBee View Post
    swgh was pointing out that pointers can be used to implement (programming language-agnostic) pass-by-reference, as one learns about it in proper programming courses.

    It wouldn't make sense to say that pointers can be used to implement pass-by-pointer. That statement contains zero information.
    Well, it didn't sound like that in the post.
    But you are correct in that it makes much more sense in saying pointers can be used to implement pass-by-reference.
    The jury agrees!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #28
    Registered User
    Join Date
    Jul 2003
    Posts
    110
    Quote Originally Posted by Elysia View Post
    You are incorrect in that references do not take storage. They do.
    I don't know what the standards say, but references are just a different way of using pointers.
    I was speaking conceptually about the differences between pointers and references, which I did not make explicitly clear in my post. Conceptually, references are not objects and do not require storage. Pointers are and do.

  14. #29
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by laserlight View Post
    Actually, it is not the same as a "null reference". It is merely a const reference to an int that is bound to an integer literal. Since const references may be bound to rvalues, this is legal (and thus compilable), as Elysia has pointed out. It is still rubbish in the sense that NULL is intended as a null pointer constant, so initialising an int reference with it is not semantically correct.
    Oh, I may have quoted the wrong post back there.

    Quote Originally Posted by rudyman
    it is possible to assign and check a reference to be null.
    Sounds like you're not getting this, though I'm sure you kinda are; You cannot check a reference for null because it cannot be null.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Variable pointers and function pointers
    By Luciferek in forum C++ Programming
    Replies: 11
    Last Post: 08-02-2008, 02:04 AM
  2. MergeSort with array of pointers
    By lionheart in forum C Programming
    Replies: 18
    Last Post: 08-01-2008, 10:23 AM
  3. array of pointers to an array pointers
    By onebrother in forum C Programming
    Replies: 2
    Last Post: 07-28-2008, 11:45 AM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM