Pointer

This is a discussion on Pointer within the C++ Programming forums, part of the General Programming Boards category; i need a pointer on pointers (pun intended). can someone explain to me how to use them and what there ...

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    28

    Arrow Pointer

    i need a pointer on pointers (pun intended). can someone explain to me how to use them and what there purpose is becuase i have read the tutorial about pointers and dont exactly understand why they are needed and i kinda understand how to use them.

    thanks alot!

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Arrays, functions(passing-by-value/passing-by- reference), and classes are all topics permeated with the use of pointers, and unless you spend the time to study each of those topics thoroughly, you aren't going to fully appreciate the "whys" of pointers, but here is a short explanation:

    Their primary purpose is efficiency. If you pass an object to a function, the compiler needs to make a copy of the object for the function. Making a copy of an object is terribly inefficient, especially for large objects. In addition, the function can only make changes to the copy it receives, and when the function ends, the copy is destroyed, so the function ends up not being able to make any permanent changes to the original object.

    On the other hand, a pointer variable stores a memory address of where the object is located and the address looks something like this:

    007E0D50

    If you pass a pointer to a function, the compiler copies the pointer and gives the copy to the function, but the effect is very different. First of all, it is very efficient to copy the address since it's so small and therefore it takes almost no time. Also,
    the object itself might be extremely massive and take up lots of memory, but the address takes up very little memory. Finally, the function is able to make permanent changes to the original object through a pointer.

    Having said all that, it's not necessary to know their purpose to learn the fundamentals of how pointers work. Think of it as a little game. Learn the game, and how it works, and after that, you will gain an appreciation of when to use them and why they are so powerful.

    Good luck.
    Last edited by 7stud; 08-03-2003 at 04:13 PM.

  3. #3
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,220
    All data is stored in memory at a specific address. So a little char like this
    char foo = 'a'

    is stored as 97 (the ascii code for a) in a byte of memory.

    This byte of memory has an address that the system uses to locate it. The address can be pretty much anything the program is allocated by the operating system (attempting to read/write/execute stuff outside of the space allocated by the operating system can lead to the operating system killing your program and throwing up an error (GPF or Segfault), because you're not allowed to do that).

    so lets say this foo's address (&foo) is 600. and we make a pointer to it called bar.

    char *bar = &foo

    Now bar itself takes up 4 bytes of memory (on a 32-bit system). bar is a pointer. All bar contains is the address of foo, 97. You can pass bar to functions and this allows functions to edit foo (thus avoiding complicated returns).

    Pointers are very important, and you'll see and use them alot. Unless you decide to program in java or something.

    misinformation is a gift.
    Last edited by Brian; 08-03-2003 at 04:10 PM.

  4. #4
    Registered User
    Join Date
    Nov 2002
    Posts
    28

    the replies

    thanks for the replies, i didnt fully understand tehm becuase i only have four month of crap prgramming knowledge. i will try to get the most out of ur posts tho.

  5. #5
    Registered User
    Join Date
    Nov 2002
    Posts
    28

    ok i got one more question about pointers

    ok so i kinda still dont understand pointers, but i am going to show u why i dont get it. in the tutorial they give you this code:

    Code:
    #include <iostream.h>
    int main()
    { 
     int x;                           //A normal integer 
     int *pointer;                    //A pointer to an integer 
     pointer=&x;                      //Read it, "pointer equals the address of x"
     cin>>x;                         //Reads in x    
     cout<<*pointer;                  //Note the use of the * to output the actual number stored in x
    }

    ok so you see i understand how all this codes works, but the things i still dont get is, well if you look at this it looks like the pointer is only being you to display the value of x in another way, couldnt this just be done easily with out the extra code just by doing:

    Code:
    cout<<x;

  6. #6
    Registered User
    Join Date
    Mar 2002
    Posts
    249
    ok so you see i understand how all this codes works, but the things i still dont get is, well if you look at this it looks like the pointer is only being you to display the value of x in another way, couldnt this just be done easily with out the extra code just by doing
    Yes, that was just a basic example of how to use pointers, but not really how pointers are used. They're just for efficiency and like the other people here said, are tied in with functions.
    Well, there are a few things wrong with your code:

    1) It does not work.
    2) It does not work.
    3) It does not work.

    Hope this helps.

  7. #7
    Registered User codingmaster's Avatar
    Join Date
    Sep 2002
    Posts
    309

  8. #8
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,136
    FAQ

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 11:10 AM
  2. Quick Pointer Question
    By gwarf420 in forum C Programming
    Replies: 15
    Last Post: 06-01-2008, 04:47 PM
  3. Parameter passing with pointer to pointer
    By notsure in forum C++ Programming
    Replies: 15
    Last Post: 08-12-2006, 08:12 AM
  4. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 04:36 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 01:04 PM

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