Thread: Pointer to pointer as a function's parameter

  1. #1
    Registered User
    Join Date
    Nov 2015
    Posts
    54

    Pointer to pointer as a function's parameter

    Hi!
    I will be grateful for your help on how a pointer to a pointer works as a parameter to a function. This pointer to a pointer topic gives me a truly hard time.

    For example, there are such lines in the main function:
    Code:
     char* some_buffer;
              int buffer_length;
              
              if (some_function(&some_buffer, &buffer_length))
    Then within some_function:
    Code:
    bool some_function (char** buffer_from_main, int* buffer_length)
    {
      if((*buffer_from_main = malloc(*buffer_length + 1)) != NULL)
                    do something and return true; else return false;
    }
    My questions:

    If I understand it correctly, buffer_from_main is a pointer to a pointer; so buffer_from_main points to the address of the pointer some_buffer (which is in the main function), and some_buffer, in its turn, points to some address, where something should be stored, or is stored (I will refer to this something as "stored_value").

    Thus if I pass char** buffer_from_main as a parameter of the function, do I pass the address of "stored_value"?

    If yes, then
    Code:
    *buffer_from_main = malloc(*buffer_length + 1)
    allocates memory not for the "stored_value" but for the pointer
    char* some_buffer? And to allocate a memory for "stored_value", I have to write
    Code:
    if((**buffer_from_main = malloc(*buffer_length + 1)) != NULL)
    correct?

    Thanks a lot for your help!

  2. #2
    Registered User
    Join Date
    Oct 2015
    Posts
    11
    Hello !

    Your problem is a bit confusing to me, maybe due to the extreme heat, but your reasoning seems correct to me.
    I advice you anyway to think of all variables as variables in all cases, even if they happen to be pointers.

  3. #3
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    buffer_from_main is a pointer to a pointer to a character, which could be the first character of an array. *buffer_from_main is a pointer to a character. **buffer_from_main is a character. The first example using if(*buffer...) is correct, storing the address of the allocated memory into a pointer to character.

  4. #4
    Registered User
    Join Date
    Nov 2015
    Posts
    54
    Quote Originally Posted by rcgldr View Post
    buffer_from_main is a pointer to a pointer to a character, which could be the first character of an array. *buffer_from_main is a pointer to a character. **buffer_from_main is a character. The first example using if(*buffer...) is correct, storing the address of the allocated memory into a pointer to character.
    Thank you for your answer. I am a bit confused though.

    Do I understand correctly that if *buffer_from_main is a pointer to a character, then in fact *buffer_from_main is some_buffer, which points to the character? If yes, then if(*buffer...) allocates memory for the character/variable and stored the address of that memory into *buffer_from_main?

    If that is correct, why do I have to use char ** buffer_from_main as a parameter of the function and not char * buffer_from_main?

    Thank you!

  5. #5
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    Quote Originally Posted by Ducol View Post
    Why do I have to use char ** buffer_from_main as a parameter of the function and not char * buffer_from_main?
    In main, some_buffer is a pointer to a character. In order to be able to modify some_buffer from a function, main has to pass the address of some_buffer to the function using &some_buffer. The called function is receiving the address of some_buffer, and since some_buffer is a pointer to character, the called function has to declare that input paramter as a pointer to a pointer to character (since it's a pointer to some_buffer, which is a pointer to a character).

  6. #6
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Why not make your question less abstract with a useful example. The reason we need a char** is so we can modify the char* in main by dereferencing the char**.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    size_t string_copy(char **dest, const char *src) {
        size_t len = strlen(src);
        *dest = malloc(len + 1);
        if (*dest == NULL)
            len = 0;
        else
            strcpy(*dest, src);
        return len;
    }
    
    int main(void) {
        char *s;
        size_t len = string_copy(&s, "make a copy of this string");
        if (s != NULL) {
            printf("%zu: %s\n", len, s);
            free(s);
        }
        return 0;
    }

  7. #7
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Do I understand correctly that if *buffer_from_main is a pointer to a character, then in fact *buffer_from_main is some_buffer, which points to the character? If yes, then if(*buffer...) allocates memory for the character/variable and stored the address of that memory into *buffer_from_main?
    Yes.
    If that is correct, why do I have to use char ** buffer_from_main as a parameter of the function and not char * buffer_from_main?
    Because you are changing what a pointer from somewhere else is pointing to and you want that change reflected in main(), not just the underlying value.

    Consider a normal integer pointer. One way to change the variable across functions is to use a pointer. The other way is to return it. What's important to understand though is that this logic extends to when you want to change pointers across functions, too. Every pointer has a value stored called an address. What you are doing is changing the address, through another pointer.

    To make it even more clear, consider the syntax: char **buffer_from_main tells you what the pointed-to type is. Another example would be
    that long int *x points to long int.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A pointer to an object's function as a parameter
    By wiiire in forum C++ Programming
    Replies: 2
    Last Post: 09-30-2007, 10:38 PM
  2. Function pointer as the parameter
    By lynnluoll in forum C++ Programming
    Replies: 3
    Last Post: 05-20-2005, 09:31 AM
  3. pointer to array as parameter to function
    By Jurgen in forum C++ Programming
    Replies: 1
    Last Post: 05-20-2004, 02:51 PM
  4. function formal parameter pointer style for arrays
    By curlious in forum C++ Programming
    Replies: 5
    Last Post: 07-08-2003, 06:18 PM
  5. function pointer & Thread Parameter :: Multithreading
    By kuphryn in forum Windows Programming
    Replies: 3
    Last Post: 09-11-2002, 08:42 AM

Tags for this Thread