Thread: pointer to pointer as function arguments

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    347

    pointer to pointer as function arguments

    I am really trying understand pointer to pointer in function arguments. I am seeing a lot in my example projects using double pointer. I want to know what is the main use of them? One example function i have seen is
    Code:
    ProvideRxBuffer(**ptr);
    I could not provide the complete implementation as it is not my project. The function is called when a data is arrived and it will ask the application for buffer. Can someone explain me with simple program how does it work?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It might be worth trying a related exercise without a pointer to pointer to see why you would need it.

    Suppose you want to implement this function to fill a dynamic array with n of a given value:
    Code:
    void fill_n(int *array, size_t n, int value);
    fill_n will call malloc to allocate space for n ints, then fill with value. It could be used like this:
    Code:
    int *array = NULL;
    fill_n(array, 10, 5);
    // print the array elements
    // ...
    free(array);
    Try to implement fill_n with the given prototype and see if you can get it to work. You will find that you need fill_n to be:
    Code:
    void fill_n(int **array, size_t n, int value);
    instead
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    Pointers are integers used as an address to the first item of an array... Consider arrays of chars... You can have a pointer char *p, pointing to a string (a simple array of chars)... By the same analogy, a pointer to pointers (char **pp, for example), points to an array of pointers, and each of these pointers points to arrays. For example:

    pointer to pointer as function arguments-untitled-png

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function Pointer Typedef with Same Type Arguments
    By SevenThunders in forum C++ Programming
    Replies: 4
    Last Post: 04-01-2015, 02:55 AM
  2. Replies: 8
    Last Post: 03-01-2015, 12:39 AM
  3. Replies: 5
    Last Post: 02-02-2013, 09:33 PM
  4. Replies: 3
    Last Post: 10-07-2011, 01:54 PM
  5. Replies: 9
    Last Post: 01-02-2007, 04:22 PM

Tags for this Thread