Thread: Pointers

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    545

    Pointers

    I have just started to go through the rest of the C++ Tutorials on this website and have just done pointers. What is the point in pointers and for what might you use them. (No Pun Intended). Some example code might be useful.

  2. #2
    Super Moderator Harbinger's Avatar
    Join Date
    Nov 2004
    Posts
    74
    Try writing say strlen() without using pointers.

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    197
    Ehh, I think something much more important then that where pointers are usefull, is datastructures, such as the linked list, which you might touch on after the pointers tutorial (I haven't actually read threw the tutorials here)
    If any part of my post is incorrect, please correct me.

    This post is not guarantied to be correct, and is not to be taken as a matter of fact, but of opinion or a guess, unless otherwise noted.

  4. #4
    Unregistered User
    Join Date
    Sep 2005
    Location
    Antarctica
    Posts
    341
    there should be tons of examples already, but the point of a pointer is to point to the location in memory of a variable, that way, if you pass a pointer to the variable to another function, and that function changes the value of that variable, you can see the change once the function has ended.

    Code:
    void f(int x)
    {
        x = 7;
    }
    
    void f2(int *x)
    {
        *x = 777;
    }
    
    int a = 10;
    f(a);
    
    cout << "a = " << a << endl;
    
    f(&a);
    
    cout << "a = " << a << endl;
    This code will print out:
    a = 10
    a = 777

    as you notice, the first function didn't actually modify the variable because you only passed in the value of the variable and it created a new variable containing that value. In the second function, you passed in the address of your variable and it used that pointer to modify the value, so when the function returned, you can see the variable changed.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Pointers are also used to pass arrays to functions:
    Code:
    void func(int *array) {
    
    }
    
    int a[10];
    func(a);
    (You may see func() declared like this:
    Code:
    void func(int array[])
    )
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    What is the point in pointers and for what might you use them.
    It's not important to know the why's at this point. For now, concentrate on learning the mechanics of how they work. Later, when you learn about functions, the why's are taught, and you'll learn the difference between "passing by value" and "passing by reference".

    However, trying to learn C++ from a bunch of one page tutorials is probably not the best way to go about it. Get a C++ book. Most C++ books are huge, so if you don't want to commit that much time and effort, you might want to try a book like "C++: A Beginner's Guide", which gives a more cursory overview of the language--and it's cheap.
    Last edited by 7stud; 11-12-2005 at 06:40 PM.

  7. #7
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Quote Originally Posted by 7stud
    However, trying to learn C++ from a bunch of one page tutorials is probably not the best way to go about it. Get a C++ book. Most C++ books are huge, so if you don't want to commit that much time and effort....
    If you don't want to commit that much time and effort, then get a different profession or change your major in college Its a little like learning to play the piano well -- can't be done without a great deal of time and practice. If you are unwilling to do that, then you might as well be playing computer games.

  8. #8
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    I am only at High School and I am learning and am willing to learn.

  9. #9
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Well as 7stud said, don't worry about why's. You're still young and have plenty of time to gain experience. Think of your programming side as a new person born inside of you. When you're little, your parents teach you all sorts of things that you should know and do and they don't tell you why. It doesn't matter at that point, you just know that they mean something and you should know them. Eventually the time will come where you need to know why and by then you'll find that you already know it.
    Sent from my iPadŽ

  10. #10
    ~Team work is the best!~ wakish's Avatar
    Join Date
    Sep 2005
    Posts
    85
    Quote Originally Posted by SlyMaelstrom
    Well as 7stud said, don't worry about why's. You're still young and have plenty of time to gain experience. Think of your programming side as a new person born inside of you. When you're little, your parents teach you all sorts of things that you should know and do and they don't tell you why. It doesn't matter at that point, you just know that they mean something and you should know them. Eventually the time will come where you need to know why and by then you'll find that you already know it.
    nice analogy!

  11. #11
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    Isn't it...i just wish someone would analyse my File Output problem in a way that I can nderstand.

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