Thread: What are pointers for?

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    12

    What are pointers for?

    I've read the/a pointer tutorial when i was 12, when i was 13, and just recently.

    All three times, I have not figured out a use, or what it does actually



    My question is, should I even bother trying to learn them? Or are they something as useless as like, the <tt> tag in html?

    because, what i've learned from tutorials, they don't do anything new..

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    What do you mean they don't do anything new? They allow for dynamic memory allocation. They allow for pass-by-reference (since they've been around before references were), which saves you a ton of overhead when dealing with large objects. The list goes on.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    They are one of the major things that set C/C++ apart from most other languages (mainstream one's at least). Learn them. They are useful.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  4. #4
    samurai warrior nextus's Avatar
    Join Date
    Nov 2001
    Posts
    196
    Quote Originally Posted by s3abass
    I've read the/a pointer tutorial when i was 12, when i was 13, and just recently.

    All three times, I have not figured out a use, or what it does actually



    My question is, should I even bother trying to learn them? Or are they something as useless as like, the <tt> tag in html?

    because, what i've learned from tutorials, they don't do anything new..
    useless? OMG.....they are far from being useless....
    nextus, the samurai warrior

  5. #5
    Never Exist Hermitsky's Avatar
    Join Date
    Jul 2004
    Posts
    149
    i have the same question when the first time i come to c++,
    now, perhaps i got the answer

    blow me ... ...

  6. #6
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    pointers a probably THE thing to learn in C/C++....i would say that any useful (good) program is going to have at least a couple of pointers...maybe not, but none the less, they're very very useful...

    the thing is, you don't really get to see the power of pointers until you start writing more dynamic programs....watch this....this is how i grasped the concept of pointer...this is a linked list...
    Code:
    struct node
    {
          int x;  
          node *prev;
          node *next;
    };
    
    int main()
    {
        node origin;
        node *current;
        node *temp;
      
        current = &origin
        current->x = 0;
        current->prev = NULL;
        current->next = NULL;
        
        current->next = new node;
        current->next->next = NULL;
        current->next->prev = current;
        current = current->next;
    
        current->x = 1;
    
    
        current->next = new node;
        current->next->next = NULL;
        current->next->prev = current;
        current = current->next;
    
        current->x = 2;
        current->next = new node;
        current->next->next = NULL;
        current->next->prev = current;
        current = current->next;
    
        current->x = 3;
    
         //i don't want the third node anymore, i'll just get rid of it
         temp = current->prev;
         current->prev->prev->next = current;
         current->prev = current->prev->prev;
         delete temp;
    }
    fun stuff......just look up linked lists...you'll *start* to see the power of pointers..

  7. #7
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Quote Originally Posted by misplaced
    pointers a probably THE thing to learn in C/C++....
    i would say that any useful (good) program is going to have at least a couple of pointers
    misplaced is a good example of a step 2 programmer.
    s3abass is at step 1.

    http://cboard.cprogramming.com/showt...eps#post393652

    Last edited by Sang-drax; 10-13-2004 at 09:24 AM.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  8. #8
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    woo hooo....step 2!!

  9. #9
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    ...
    Finally, you realize that pointers don't need to be used much in C++. You use references to pass values to functions and STL containers to dynamically allocate memory. Your code is easy to read and very robust.
    so i guess a vector doesn't use pointers? (rhetorical)

    besides, i meant that as pointers are what sets c/c++ aside from the rest....

    but i'm happy with being a step 2.

  10. #10
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    so i guess a vector doesn't use pointers? (rhetorical)
    I know its rhetorical but to answer: Only the implementation knows (aside from the return types of some functions )

  11. #11
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    damn you black boxing!!...damn you!!

  12. #12
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    I think that pointers - raw pointers, that is - are kinda useless to C++ newbies. What for? Strings? Use std::string. PassByReference? Use references. Dynamic Memory? Use smart pointers.
    The biggest advantage C++ has over C for newbies is that you don't HAVE to use pointers. Which is why I think that a good from-the-grounds-up C++ tutorial shouldn't until late.
    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

  13. #13
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Quote Originally Posted by CornedBee
    I think that pointers - raw pointers, that is - are kinda useless to C++ newbies. What for? Strings? Use std::string. PassByReference? Use references. Dynamic Memory? Use smart pointers.
    The biggest advantage C++ has over C for newbies is that you don't HAVE to use pointers. Which is why I think that a good from-the-grounds-up C++ tutorial shouldn't until late.
    You're a step 3 C++ programmer

    Seriously, I couldn't agree more. You should learn string, containers, references, etc. before using pointers.
    What you just said is IMO what many people don't get about C++. Every C++ book for newbies I've seen has pointers and char[] quite early and containers as a late chapter or even as an appendix.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  14. #14
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    >> Every C++ book for newbies I've seen has pointers and char[] quite early and containers as a late chapter or even as an appendix.
    Well, you have to learn the fundamentals to know how everything works. Once in awhile in my Comp Sci courses, I still have to create my own linked list and stuff when stl is available. You know like in calculus class you do everything by hand but then in practical, real life, you use a tool to generate the solution for you
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  15. #15
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    There is a difference between teaching somebody how something works and teaching them to do it the hard way. Too often C++ classes and books teach people to code the "wrong" way. In my opinion, it is far more beneficial to teach more appropriate techniques first, then go into finer detail on why those techniques work. This is why I would expect someone to learn C++ before assembly.

    Too often I see newbies (including myself at one point) who are confused with pointers and low level techniques and have a hard time getting anything done, and then struggle to break the habits they picked up once they move on to more "advanced" topics.

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, 10:33 AM
  4. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 01:18 PM