Question about the introduction to pointers lesson

This is a discussion on Question about the introduction to pointers lesson within the C++ Programming forums, part of the General Programming Boards category; Alright, so I've just read through lesson 6: An Introduction To Pointers on the website, and then skimmed through it ...

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    7

    Question about the introduction to pointers lesson

    Alright, so I've just read through lesson 6: An Introduction To Pointers on the website, and then skimmed through it again after I was done to help soak it all in. While I don't expect to fully grasp the concept immediately, I am a bit confused as to the usefulness of pointers. From what I pulled from the text, and especially from the example code I can't really see any reason to use them. Take the short sample program for instance: Why go through all that trouble when I could have just used x instead of *p in the final cout? Again, I am quite positive I am missing a bigger picture here, and excuse me for that. I'm trying my best to take all of this in slowly.

    Oh also, this is my first post and hopefully there will be many more to follow. So uh, hello to everyone.

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    I am a bit confused as to the usefulness of pointers.
    The purpose of a beginning lesson on pointers is not to understand why, but rather to learn the mechanics of using pointers. The reasons pointers are useful comes later.

    In addition, if you are really interested in learning C++, then reading a bunch of 1 page tutorials is not the way to go. My beginning C++ book is 900 pages long. Why do you think that is? Why didn't they just write 30 one page tutorials for the book and dispense with the other 870 pages?
    Last edited by 7stud; 02-15-2006 at 03:01 PM.

  3. #3
    Registered User
    Join Date
    Feb 2006
    Posts
    7
    Good point. I'm doing the best I can with what I have at the moment, and I've looked through a gob of tutorials on the internet and so far these have been the most helpful. If I really should head out and pick up a book though, what do you recommend?

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    If I really should head out and pick up a book though, what do you recommend?
    It depends on how smart you are and how much time you want to commit. Every book is enthusiastically recommended by some people and trashed by other people.

    For a briefer overview that's not too difficult, I would recommend the Beginner's Guide books for almost any language, e.g. "C++: A Beginner's Guide".
    Last edited by 7stud; 02-15-2006 at 08:24 PM.

  5. #5
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    if you are interested in the usefulness of pointers, it was
    explained to me best as allowing a function to return more than
    one value to the function that called it. the thing is, that as
    you should understand from the tutorial, pointers allow you to
    modify the contents of memory locations directly.

    when you call a function, and say pass it an int value, what the
    computer does is make a copy of the value being passed and
    stores it in a new local function variable. however if you passed
    a pointer to the int (assuming the receiving function is designed to
    take a pointer), you actually pass the memory address of the
    original variable, and dereferencing the pointer allows you
    to modify the original data.

    earlier i said return multiple values, its not quite the same as
    that, but you should get the idea of the implications of such a
    thing. the prime use of pass by reference as it is known (although
    from reading another thread in which 7stud posted, he might
    disagree, but at this point, there is no point on fussing over the
    detail of what happens when you pass by reference), the real
    use of pointers is that they avoid need for global variables, and
    allow passing/returning arrays. one last thing, i'm not trying to
    say that pointers and arrays are the same. i've probably
    confused the issue by trying to avoid criticism for what i'm
    saying, but you should see that pointers are useful from this
    all the same.
    are the same thing, i just wan to clarify that before i
    leave the point.
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    >> e.g. "C++: A Beginner's Guide".

    Every time I see 7stud recommending this book, I recommend against it. If you want to learn better, more modern C++, try Accelerated C++. There are many discussions available through search about which book is best.

  7. #7
    Registered User Kurisu's Avatar
    Join Date
    Feb 2006
    Posts
    62
    since most people want to program for games perhaps this example might make sense:

    Say you load an image of a monster to use in your game. This image is stored in memory.

    Say your game will have 5 monsters that will use this one image. Instead of making 5 copies of the image as regular variables would do (5x as much memory used) you instead have each monster use a pointer so that all 5 monsters point to a single image. (1x memory usage)

    Pointers are also good for making structures such as linked lists or dynamically allocating of memory.

    i.e. A program that may store anywhere from 1 to 1000 values.
    Code:
    int a[1000];  // Big enough to hold worst case scenario
    vs
    Code:
    > How many values this time Ernie? (userInput)
    > userInput = 25
    --------------------
    int *a = new int[userInput]; // Array size determined at runtime and is only as big as necessary.
    From personal experience these suckers come in useful.
    Last edited by Kurisu; 02-15-2006 at 05:24 PM.

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    Pointers are useful, but they tend to be much more useful in C than in C++. In C++ there are often alternatives that are more appropriate. For example, instead of using new to dynamically allocate the array in Kurisu's code, you should use vector in C++ that doesn't require knowledge of pointers. Many tutorials and books teach C style C++, which is why pointers tend to be taught earlier than necessary, but you will need to understand them soon enough anyway.

  9. #9
    Registered User
    Join Date
    Mar 2002
    Posts
    203
    Quote Originally Posted by Daved
    ...you should use vector in C++ that doesn't require knowledge of pointers...
    But wouldn't a vector of pointers to a class run much faster than a vector of the class. For some situations this probably isn't correct, but if the vector is very large, the class is large, and the copy constructor is intensive then wouldn't using pointer increase performance by quite a bit?

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    In some cases, if copying is expensive or you want to take advantage of polymorphism, a vector of pointers is appropriate, although in C++ a vector of shared_ptr's (or a ptr_vector) is an even better choice for that situation which again doesn't really require the use of raw pointers.

    Once you get to such an advanced topic, a knowledge of raw pointers and how they work is important even if you don't use them directly, but like I said there are often better alternatives to raw pointers in C++.

  11. #11
    Registered User
    Join Date
    Feb 2006
    Posts
    7
    Ahh. That makes a good bit more sense. Thanks a bunch! Glad I found my way here. So, I doubt I'll be putting any of this to good use for a long while, but it's nice to have this somewhat eased over in my head.

  12. #12
    Registered User
    Join Date
    Feb 2006
    Posts
    155
    poitner is just an indirection,its just a way which allows u to access variables by their addresses and not by the variable names.
    since a variable name is english u cant add a 1 or a 2 to it.
    but an address is a number,if u add 1 to it u get to the next address and add nother one u get to the next address.
    so with just one pointer u can access a lot of memory,u just have to use a loop along with the pointer.
    u can increment pointers,decrement pointers .each time u increment or decrement that gets u access to a different part of memory.

    its like an array,array and pointers have alot in common,go through that.
    pointers allow u a " call by refrence" .
    and there r pointers of all sorts,pointers to variables,pointers to structures.

    check this out:

    char string[10]="akljsdlfk"

    with pointers
    char *string ="akjdslfksd"



    u just need one pointer variable here,so 'a' is stored at string

    'k' is stored at (string+1) ,'j' is stored at (string+2) and so on.
    Last edited by qqqqxxxx; 02-15-2006 at 10:48 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A question about pointers in functions
    By occams razor in forum C Programming
    Replies: 3
    Last Post: 05-15-2007, 12:16 AM
  2. Newbie question about pointers in function parameters.
    By sojurn in forum C++ Programming
    Replies: 14
    Last Post: 01-20-2007, 08:21 PM
  3. simple pointers question
    By euphie in forum C Programming
    Replies: 4
    Last Post: 05-25-2006, 01:51 AM
  4. a question with pointers
    By louis_mine in forum C Programming
    Replies: 4
    Last Post: 02-09-2005, 02:15 PM
  5. Quick Question Regarding Pointers
    By charash in forum C++ Programming
    Replies: 4
    Last Post: 05-04-2002, 11:04 AM

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