Thread: Pointers to pointers

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    46

    Pointers to pointers

    What are they really good for? In every case of pointers that I've seen, just one pointer would work fine yet the programmer chose a pointer to a pointer. Why would I want to use that instead of just one pointer when one would work fine?
    C code. C code run. Run code, run...please!

  2. #2
    AndersSundman
    Guest

    Talking p2p

    Sometimes you wan't to pass a pointer to a function as a reference (so that the pointer value can be changed).

    Say that the pointer points to the pixels in an image and you have a function for loading an image from disk imgLoad(), this function might take as an argument a pointer to a pointer..

    imgLoad(void** pixels);

    This way you can allocate memory inside the function, and make the void* pixels pointer point to the new memory.. caling the function with:

    PIXELS* myPixels;
    imgLoad(&myPixels);

    This can be usefull sometimes..

    Besides that double arrays are a good example, as pointed out by the vVv. Note that you don't have to make all row's in a double array equaly long.. You can have one loking like:

    ******
    ***
    *****
    **

    This is hard to do with [row*width + col] in a single array beeing used as a double..

    - Anders

    - Anders

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, 10:33 AM
  4. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 01:18 PM