The use of pointers...

This is a discussion on The use of pointers... within the C++ Programming forums, part of the General Programming Boards category; I understand how to use pointers and all but why would you use them? Why would you use a pointer ...

  1. #1
    Not stupid, just stupider yaya's Avatar
    Join Date
    May 2007
    Location
    Earthland
    Posts
    204

    Thumbs up The use of pointers...

    I understand how to use pointers and all but why would you use them? Why would you use a pointer to a variable when you can just use the pointer? The only reason I've found is to use variables in different functions (if they're not global).

    Thanks in advanced.

  2. #2
    Registered User
    Join Date
    Jul 2007
    Posts
    5
    what do you know about pointers?

  3. #3
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,638
    C++ is not language xyz. Other languages don't need pointers because, by design, that language wouldn't need to indirectly refer to an object through a memory address alone. C++ on the other hand is different. A pointer is the only way to know where something is on ocassion.

  4. #4
    Registered User
    Join Date
    Jul 2007
    Posts
    5
    can you tell me more about pointers, cause im sort of lost with them too

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> I understand how to use pointers and all but why would you use them?
    This exact question is asked a lot. Here are two older threads that try to answer it and include some other relevant discussion:

    Why pointers? (The answer)
    What are pointers for?

  6. #6
    Registered User
    Join Date
    Jul 2007
    Posts
    5
    the only time i can see a use for a pointer, i could probably do the same with a reference

  7. #7
    Not stupid, just stupider yaya's Avatar
    Join Date
    May 2007
    Location
    Earthland
    Posts
    204
    Thanks a bunch. I still don't 100% understand but I read in one of those links that they are only really important for more "dynamic programs". So long as I know how to use them (not when), that should be sufficient for now.

  8. #8
    Registered User
    Join Date
    Apr 2006
    Posts
    2,021
    Quote Originally Posted by Hobgoblin View Post
    the only time i can see a use for a pointer, i could probably do the same with a reference
    Good luck iterating through an array with a reference. References cannot change the object they refer to. Pointers can.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  9. #9
    Registered User
    Join Date
    May 2007
    Posts
    88
    > References cannot change the object they refer to.

    Oh no? Then explain the output of this program...

    Code:
    #include <iostream>
    
    void foo(int& bar)
    {
    	bar = 1;
    }
    
    int main()
    {
    	int bar = 0;
    	foo(bar);
    	std::cout <<bar <<std::endl;
    }

  10. #10
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    Quote Originally Posted by UMR_Student View Post
    > References cannot change the object they refer to.

    Oh no? Then explain the output of this program...

    Code:
    #include <iostream>
    
    void foo(int& bar)
    {
    	bar = 1;
    }
    
    int main()
    {
    	int bar = 0;
    	foo(bar);
    	std::cout <<bar <<std::endl;
    }
    Not change the contents of the object, but start referencing another object
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  11. #11
    Registered User
    Join Date
    May 2007
    Posts
    88
    Yeah, I totally read that wrong. Sorry.

  12. #12
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Posts
    311
    Languages such as Java, for example, have no pointer data type that the programmer can explicitly declare. However, behind the scenes, most everything is actually a pointer. Learn some assembly language and you will see that.

    In C++, virtual functions can be used to replace function pointers. But, there is really still a function pointer in there. I believe the goal is to reduce complexity and potential errors by the programmer. But there is something to be said for a programmer who learns how everything works behind the scenes.

    And there are some instances in C++ where a reference will not do. For example, references (which are really just a const pointer, logically), cannot change what they point to. Even as a amateur programmer, I have many times needed to have a pointer change what it's pointing at.

  13. #13
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    902
    I came to appreciate pointers more as I learned more. I think once my programming branched out from a sequence of commands and if-statements, I saw the need for pointers.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  14. #14
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    Depending on the book/tutorial you're using to learn C++, you could be forgiven for thinking that pointers are a novice-beginner subject (Far too many books & tutorials present them this way) - in fact, the language standard goes to great lengths to protect beginners against the need for pointers until much later on (Not so for 'C' where pointers are more of a bread and butter issue) The availability of references and STL greatly reduces their practical use in simple programs, leaving pointers for more advanced topics, such as the following;

    * More in-depth OO techniques such as polymorphism and (GoF) design patterns
    * Dynamic memory allocation (Something which is best avoided in C++ when possible - although plenty of old-style tutorials insist on teaching this before delving into the STL)
    * Manipulating 'raw' arrays (Ideally, a beginner will already have a firm idea about STL Containers & Iterators before doing this - again, old-style tutorials usually do this the other way around)
    * Dynamic data structures - eg, linked lists, binary trees, etc

    If these concepts fly over your head, then you're probably not in a position yet where pointers are of any significant use to you. IOW, don't worry about them just yet - focus your effort on understanding the basic language features and the STL for now

  15. #15
    Registered User
    Join Date
    Nov 2006
    Posts
    519
    I don't see where c++ wants to protect the programmer from pointers. If it would do so there would be

    Code:
    int main(vector<string> argv);
    
    // instead of
    
    int main(int argc, char** argv);
    So you need to know about pointers for the simplest possible c++ program.
    And even if the first version of main would exists one wouldn't be able to list all command line arguments without dereferencing the iterator.
    Better get pointers right.

    http://www.joelonsoftware.com/articl...vaSchools.html

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using pointers to pointers
    By steve1_rm in forum C Programming
    Replies: 18
    Last Post: 05-29-2008, 05:59 AM
  2. function pointers
    By benhaldor in forum C Programming
    Replies: 4
    Last Post: 08-19-2007, 10:56 AM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  4. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 12:18 PM

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