Thread: Can anyone kindly explain to me what exactly are POINTERS?

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    3

    Smile Can anyone kindly explain to me what exactly are POINTERS?

    Hi everyone.

    I am a amateur programmer with experience in classic ASP, VB6, Java.

    Recently, i began to read up on C++ as i realised i am missing a lot of stuff if i do not take up C++. Notwithstanding the emerging of other languages and technologies, i am willng to take up a language that is the building block of major applications.

    Due to my knowledge of java, i have no problem in dealing with C++'s looping and control structures. But when i reach upon the section of Pointers, it stuns me.

    I dont really understand the concept of pointers. It would be good if someone can kindly explain it to me. Many thanks.

    To me, pointers looks similiar to Java's way of initialising a new object and some sort of referencing. The way of deleting a pointer looks like classic ASP's way of destroying an object.

  2. #2
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Basically, when you call the 'new' operator in C++, a block of memory of specified size is set aside. For example, 'new int' sets aside enough memory for one int, or 'new double[10]' sets aside enough memory for 10 doubles. Then, new returns a pointer. The pointer is a variable containing the starting address of that block of memory. When you dereference the pointer, then you are accessing the memory that the variable points to. Well, thats pointers in a nutshell... hope that helps somewhat.
    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.

  3. #3
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490
    syntax:
    char* p;
    char p;

    the first is a pointer to a char type data value. The second is a char type data value.

    char* p;
    p contains the address in memory where the char value is stored. Sometimes this address can be bogus, which frequently causes program crashes.

    assuming you have allocated some memory for p,
    *p = 3;
    would set the value pointed at by p equal to 3. the '*' operator dereferences a pointer into an object or a data value.

    Pointers aren't compatable among different types. int * != char *, for instance.

    Derived d;
    Base* b = &d;

    Derived is the class which derives from Base. A Base pointer can be used with any objects derived from it (and itself, too).


    For more depth, look into a chapter or two on pointers. Or, ask a follow-up question.

  4. #4
    'AlHamdulillah
    Join Date
    Feb 2003
    Posts
    790
    also, pointers can be used to get a grab at certain memory sections, like the display memory to write directory to it.

  5. #5
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    In case you don't know/haven't figured it out:

    You use pointers so that one part of your program (i.e. a certain function) can access and change information from another part of a program. So, say you want to change the value of a variable that is declared in main, but you are changing it in a function:
    Code:
    void change(int *p)
    {
    //note that when you dereferenc (*) you are changing what that pointer is pointing to
    //otherwise you would be trying to change the memory address
    if (*p>100)
    *p/=2; //divide by two
    else if (*p<100)
    *p*=2; //multiply by two
    
     delete p; //clear pointer from memory
    
    }
    
    int main
    {
     int x=5; 
           int *p;
           p=&x; //p = memory address of x
           cout<<x; 
           change(p);
           cout<<"\nx after change:"<<x;
          
    }
    oh, and in many cases using references is better and just plain easier

    instead of passing int* p you can pass "int &p" meaning you would do "change(x)" in main instead. then in the function change p as if it were x itself.
    Last edited by JaWiB; 06-11-2003 at 08:33 PM.
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  6. #6
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    One other thing about pointers, you have to explicitly create them (usually usign new), and then explicitly delete them (using delete). If the pointer goes out of scope before memory is freed, then the block of memory corresponding to that pointer cannot be recovered until the program terminates.

    Also, you might want to check out the thread on structs on this board.
    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.

  7. #7
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Read the FAQ
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by Zach L.
    One other thing about pointers, you have to explicitly create them (usually usign new), and then explicitly delete them (using delete).
    No. You do not create pointers explicitly. You create what they point to explicitly if you want to dynamicly allocate objects. If you just want a pointer to point to an exisiting object, you do not call new and delete on it.
    Code:
    data_type *ptr;
    data_type instance;
    
    ptr = &instance;
    See the above example. You wouldn't be creating an instance of an object (with a new call) here, because you just want a pointer to point to an already exisiting object.

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

  9. #9
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Right... Good point. You explicitly create the pointer, but don't have to with the objects they point to.

    If you have something like quzah's code, you should not call delete on it... if you do, you will likely have some problems.
    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.

  10. #10
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    A pointer is a variable that is assigned an address in memory where some data is located. So, it "points" to a location in memory.

    OK, so why do you need them?
    1)Suppose you have data you want to pass to a function, and the data item is huge. Normally when you pass data to a function a copy is made, and then the function gets the copy. For large data items, copying takes time, and the larger the data item the more time it takes to copy it. If instead you pass a pointer to the function, the 4 byte(?) address where the data is stored is copied and sent to the function which is much more efficient than copying the whole data item.

    2) Also, if a function tries to make changes to the data item it received, it makes changes to the copy it received--not the original data item. When the function ends, the copy is destroyed, and the original data item remains unchanged. If you pass a pointer to the function, the pointer is copied but it still points to the same location, so the function can make changes to the original data item. When the function ends, the copy of the pointer is destroyed, and the original data item remains altered.

    So, pointers are very efficient means of passing data around, and they allow you to do things to the data they point to that you couldn't do without them.

    The subject of dynamically allocating memory for data using the "new" operator, as mentioned in previous posts, is the next step after understanding basic pointers.

    Here is a tutorial on pointers(beware of the slightly broken English) with diagrams that might make it clearer:

    http://www.cplusplus.com/doc/tutorial/tut3-3.html

    There are lots of tutorials on pointers, so if that one doesn't make sense to you, find another one.
    Last edited by 7stud; 06-12-2003 at 12:38 AM.

  11. #11
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Also, if you use Java, there ARE pointers in it, implicitly. Java ONLY allows objects to be stored and copied as pointers (actually, "smart pointers" because of the garbage collection features).

    Here's another place you need pointers; suppose you were coding a binary search tree (not that you'd need to, because the STL probably has the features you need). You'd probably do something like this, coming from Java:

    Code:
    class Node{
        /* Other stuff */
        Node parent;
        Node left;
        Node right;
    };
    Unfortunately, this is wrong. This says that three Node objects are *contained* in every Node object. So, you could say that:

    sizeof(Node) = 3 * sizeof(Node) + sizeof(/* Other stuff*/ )

    You will see there is no finite size that will work; Node becomes of infinite size. There's actually a similar yet related reason it won't work -- at the line "Node parent;", the compiler needs to know sizeof(Node), but it can't know it until the end "}" of Node.

    The solution? Pointers. Now, instead of each Node object containing three Node objects, it contains *pointers* to Node objects. A pointer is usually (and in this case, it is) smaller than the object it points to. Pointers are just memory addresses, not whole objects. Further, sizeof(Node *) is constant and known, the compiler doesn't need to know anything about Node (except that Node is the name of a class) to know sizeof(Node *).

  12. #12
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398

    Here's an analogy...

    Lets say Sally has the answers to the homework assignment, but you don't know that.

    Bob doesn't have the answer, but he knows who does.

    You know that Bob can get the answers.

    OK, Bob is a pointer. He points to the answer.

    If you ask Bob what information he has, he will answer 'Sally"... Usually, that's not what you want, so you use "*Bob" to "dereference" the pointer. He will get you the answer without telling you where he got it.

    Adding to what 7stud said - Since functions can only return one value, pointers are often used wen more than one value needs to be changed by a function.

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by Cat
    Also, if you use Java, there ARE pointers in it, implicitly. Java ONLY allows objects to be stored and copied as pointers (actually, "smart pointers" because of the garbage collection features).
    Your example is really irrelevant, because we're not talking about Java here. Everything in Java is a pointer. You use the new operator in Java to create every instance of anything. This is C++ they're discussing, so the whole Java aspect is really just a confusion to the original poster.

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

  14. #14
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Yes, and the original poster said he understood Java, which means (in one sense) he should already understand pointers, which I pointed out.

  15. #15
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    It looks like fused is long gone anyway.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Hey guys..need help on pointers
    By Darkozuma in forum C++ Programming
    Replies: 5
    Last Post: 07-25-2008, 02:57 PM
  2. Using pointers to pointers
    By steve1_rm in forum C Programming
    Replies: 18
    Last Post: 05-29-2008, 05:59 AM
  3. Pointers, useful? Please explain!
    By wildex999 in forum C++ Programming
    Replies: 16
    Last Post: 02-23-2006, 11:48 AM
  4. Pointers on pointers to pointers please...
    By Morgan in forum C Programming
    Replies: 2
    Last Post: 05-16-2003, 11:24 AM
  5. API "Clean Up" Functions & delete Pointers :: Winsock
    By kuphryn in forum Windows Programming
    Replies: 2
    Last Post: 05-10-2002, 06:53 PM