This is a discussion on pointers within the C++ Programming forums, part of the General Programming Boards category; i am having trouble understanding what a pointer is and what i would ever use it for....

  1. #1
    Registered User
    Join Date
    Jun 2005


    i am having trouble understanding what a pointer is and what i would ever use it for.

  2. #2
    #include<xErath.h> xErath's Avatar
    Join Date
    Jun 2004
    seach the board.. that question is constantly being made !

  3. #3
    Registered User
    Join Date
    Apr 2003
    When you are first learning about pointers, don't worry about what you would use them for. Concentrate on understanding how they work:

    All variable names and their corresponding values are stored in memory somewhere. The location in memory where they each reside is denoted with an address. An address is somewhat scary looking:


    but you can just think of it as a mailbox number. If you look inside the mailbox with that address, you'll find the variable name and its correspoding value. For instance, if you declare an int:

    int num = 10;

    that line stores the variable name "num" with a value of 10 somewhere in memory. We don't know where it is in memory yet. However, there is an "address of" operator which will get the address of num, e.g.


    You can store that address in a special variable called a pointer:

    int* p;

    That declares a variable named p, and p is a variable of type "pointer to int". Essentially, you read the line in reverse. Now, you can assign the address of any int to p:

    p = &num;

    which reads: "p equals the address of num".

    Finally, you can take a pointer and get the value stored at that address by putting a little star in front of the pointer name:


    The "why's" of using pointers become clear when you learn about functions and classes.
    Last edited by 7stud; 06-14-2005 at 11:21 PM.

  4. #4
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    As 7stud has said, the 'how' of pointers is most important, otherwise you won't understand the 'why' even if someone explained it I'll mention some 'whys' anyway, for later reference when you've got the 'how' figured out.

    >>seach the board.. that question is constantly being made !
    I totally agree with you on that xErath, but for some reason I can't find any threads that deal with the 'why' of pointers. As such, I shall mention a few here:

    1) Pointers allow you to create objects dynamically, on the heap, which do not get cleaned up automatically - which is both useful and dangerous. Useful for stuff like use in STL containers, dangerous because you can VERY easily forget to delete them after you're done, creating memory leaks.

    2) Pointers may be used for passing "by reference" to a function. This is important in C, but in C++, references largely replace pointers in this capacity. Stylistically though, some people prefer to use pointers because it makes it immediately clear that the function *may* modify the variable.

    3) Stemming from (2), passing "by reference" also means that if the parameter you're passing to the function is a large and complex structure, a copy won't have to be made of the structure, which can give a nice speed and memory saving.

    4) Pointers can be seen as a sort of pathway to the variable you want. This concept is useful in data structures like linked lists and trees, etc... In each "node" of a linked list, there is (a)data, and (b)a pointer to the next node, until you get to the end where there is a NULL pointer. If you wanted to store the next object instead of just a pointer to the next object, in each node, you'd have a problem where the first node is infinitely large (node contains another node containing another node etc...) - and you'd also get a compiler error. Same idea with trees.

    5) People will commonly mention 'dynamic arrays' as another reason to use pointers. However, in C++, generally the use of std::vector replaces the use of dynamic arrays. Of course, std::vector itself is almost certainly built on top of dynamic arrays, but that's no reason to use dynamic arrays when you don't need to. Of course, it's still always good to understand how to use them, in case vectors are ever unavailable to you...

    Good luck
    Last edited by Hunter2; 06-14-2005 at 09:49 PM.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

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