pointers

This is a discussion on pointers within the C++ Programming forums, part of the General Programming Boards category; ok...so far I know what pointers do and how they are done: Code: // I'm not not sure if I ...

  1. #1
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052

    pointers

    ok...so far I know what pointers do and how they are done:
    Code:
    // I'm not not sure if I have put the splats (*'s) in the right places, so correct me if i'm wrong...
    int thing;
    int* pointer; // declare "pointer" a pointer
    pointer = *thing; // make it point to "thing"
    pointer* = 10; // store 10 in what it points to
    If I have written the code correctly, it should work. What i'd like to know is why pointers are needed when it is just as easy it do the following:
    Code:
    int thing = 10;
    Thanks
    -Chris
    Please direct all complaints regarding this post to the nearest brick wall Have a nice day.

  2. #2
    Unregistered
    Guest
    Code:
    int thing;
    int* pointer; // declare "pointer" a pointer
    pointer = *thing; // make it point to "thing"
    pointer* = 10; // store 10 in what it points to
    that's wrong, here's how it should be
    Code:
    int thing;
    int *pointer; //declare a pointer. 
    pointer = &thing;  //a pointer points to an address, not a value.
    *pointer = 10;  //assigns 10 to thing
    where the * is really doesn't matter, but i put it in the places that are most common. the msot common place is easier to figure out than something wierd.

  3. #3
    Frustrated Programmer :( phantom's Avatar
    Join Date
    Sep 2001
    Posts
    163
    If I can use an analogy I will attempt to explain why pointers are used and necesary ->
    Imagine a room of ten computers, if you wanted to copy information from one computer to another you would have to put the information on one disk and then put it onto your computer. A pointer is the same as putting those same ten computers on a network. Makes it a lot easier, a lot faster and there is no need for a second copy of teh info so it saves room. BHope that explains it
    PS Sorry if the above confuses you, but thats how I learnt it.
    My site to register for all my other websites!
    'Clifton Bazaar'

  4. #4
    Unregistered
    Guest
    Such simple applications are rarely the application for which pointers are used.

    Things like dynamic libraries, message maps(I think), and dynamic memory allocation would be impossible if it weren't for pointers.

  5. #5
    Frustrated Programmer :( phantom's Avatar
    Join Date
    Sep 2001
    Posts
    163
    I agree it's a very simple application but it's a beginers answer to the question of 'why do we have pointers?'. When I started programming I also asked the same questino and the analogy made it a lot easier for me
    My site to register for all my other websites!
    'Clifton Bazaar'

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. Replies: 4
    Last Post: 12-10-2006, 06:08 PM
  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