When do I specifically use Pointers?

This is a discussion on When do I specifically use Pointers? within the C++ Programming forums, part of the General Programming Boards category; I don't know when to use them. :X I know what they are and everything, but I don't know exactly ...

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    141

    When do I specifically use Pointers?

    I don't know when to use them. :X I know what they are and everything, but I don't know exactly WHEN to use them, when to use it with a variable/function. etc

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,451
    First, you have read the cprogramming tutorial on pointers, right?

    Also, you have used say, std::vector before, correct? Have you considered how std::vector might be implemented?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    141
    Quote Originally Posted by laserlight View Post
    First, you have read the cprogramming tutorial on pointers, right?

    Also, you have used say, std::vector before, correct? Have you considered how std::vector might be implemented?
    Pointers, yes someone told me about vectors being generalized as pointers.


    btw
    If you happen to have a huge piece of data that you want to pass into a function, it's a lot easier to pass its location to the function than to copy every element of the data!
    So is this a reason people use pointers?


    Sorry if I made a few threads back about pointers, it's just a confusing topic at some parts. :<
    Last edited by bobbelPoP; 08-01-2008 at 12:33 AM.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,451
    So is this a reason people use pointers?
    Yes, though in C++ we would often pass a reference instead of a pointer in such a case.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    May 2008
    Posts
    141
    Quote Originally Posted by laserlight View Post
    Yes, though in C++ we would often pass a reference instead of a pointer in such a case.
    but a reference is just referencing the memory address to a variable, is it different than making a memory address to a variable??? :/ I thought everything was in memory. :P

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,452
    See references as a simplified syntax for pointers.
    The only differences are
    - A reference is always bound to a variable - it cannot be NULL.
    - A reference cannot be rebound. Once it's bound, it can never be changed.
    - References do not require * to dereference.

    Basically, if a function requires something by reference, it means that argument isn't optional, as opposed to pointers where you can always pass NULL.
    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.

  7. #7
    Registered User
    Join Date
    May 2008
    Posts
    141
    Quote Originally Posted by Elysia View Post
    See references as a simplified syntax for pointers.
    The only differences are
    - A reference is always bound to a variable - it cannot be NULL.
    - A reference cannot be rebound. Once it's bound, it can never be changed.
    - References do not require * to dereference.

    Basically, if a function requires something by reference, it means that argument isn't optional, as opposed to pointers where you can always pass NULL.

    Code:
    - A reference is always bound to a variable - it cannot be NULL.
    Is that why I get the error, you must initialize the variable to be a reference? :P

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,452
    Pretty much, yes. A reference must always be bound to something, so just like const, it must be initialized.
    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.

  9. #9
    Registered User
    Join Date
    May 2008
    Posts
    141
    So lets, you get a massive amount of data from a file, would you put that into pointer? Or would the file system do that for you and you just have to reference it????

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,451
    See references as a simplified syntax for pointers.
    I suggest thinking of references as aliases instead. A reference is another name for an existing object. Consequently, it must refer to an existing object, unlike a pointer. Pointer syntax does not come into the picture, simply because a reference is not a pointer.

    So lets, you get a massive amount of data from a file, would you put that into pointer?
    You cannot put anything other than an address or zero into a pointer. You could put the data into memory, and then use pointers to access the memory.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    May 2008
    Posts
    141
    Quote Originally Posted by laserlight View Post
    I suggest thinking of references as aliases instead. A reference is another name for an existing object. Consequently, it must refer to an existing object, unlike a pointer. Pointer syntax does not come into the picture, simply because a reference is not a pointer.
    Are references strict to classes/structs?

  12. #12
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,452
    Quote Originally Posted by bobbelPoP View Post
    So lets, you get a massive amount of data from a file, would you put that into pointer? Or would the file system do that for you and you just have to reference it????
    It really depends on how where/how you get/store the data.
    Is it a typical raw buffer? Then a pointer might be a good choice so you can use it as an array.
    If you store it in a vector or stuff, then it's better to pass via reference.

    Quote Originally Posted by laserlight View Post
    I suggest thinking of references as aliases instead. A reference is another name for an existing object. Consequently, it must refer to an existing object, unlike a pointer. Pointer syntax does not come into the picture, simply because a reference is not a pointer.
    Ah, that's another good one. An alias it is!

    Quote Originally Posted by bobbelPoP View Post
    Are references strict to classes/structs?
    No, references can be used on any type. Just append & after it.
    BYTE&
    int&
    MyClass&
    std::vector&
    etc
    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. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,451
    Are references strict to classes/structs?
    No. In this context, when I say object, I mean "a contiguous region of memory holding a value of some type" or "a named or unnamed variable of some type".
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    Registered User
    Join Date
    May 2008
    Posts
    141
    Quote Originally Posted by laserlight View Post
    No. In this context, when I say object, I mean "a contiguous region of memory holding a value of some type" or "a named or unnamed variable of some type".
    Sorry, I come from PHP where objects are just variables to classes.

    PHP is like a very simplified C :P

  15. #15
    Registered User
    Join Date
    May 2008
    Posts
    141
    Another example is, what if you had a game physics engine, and you were trying to define skeleton bone structures ( :P ) for ragdolls, would you store any of that into a pointer?

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 12:18 PM
  2. API "Clean Up" Functions & delete Pointers :: Winsock
    By kuphryn in forum Windows Programming
    Replies: 2
    Last Post: 05-10-2002, 06:53 PM
  3. Pointers pointers pointers...
    By SMurf in forum C Programming
    Replies: 8
    Last Post: 10-23-2001, 04:55 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21