why and when is a double-pointer required?

后端 未结 3 1832
野性不改
野性不改 2021-01-16 04:40

I have been a sysadmin most of my life, but recently decided to practice some my dev knowledge and attempt a devops position. I have as such been practicing some C and Pytho

相关标签:
3条回答
  • 2021-01-16 05:22

    In C, arguments to function are passed by values. Even pointers are passed by values.

    For example:

    #include<malloc.h>
    #include<stdio.h>
    int allocatingMemory(int* ptr)
    {
        ptr = malloc(sizeof(int)); 
        if(ptr==NULL)
            return -1;
        else 
            return 0;
    }// We are not returning the pointer to allocated memory
    
    int main(void)
    {
        int* ptr;
        int allocated = allocatingMemory(ptr);
        if(allocated == 0)
        {
            *ptr = 999;// Boom!!! 
            free(ptr);
        }
    
        return 0;
    }
    

    To overcome this issue, we use

    int allocatingMemory(int** ptr)
    {
        *ptr = malloc(sizeof(int));
        if(*ptr == NULL)
            return -1;
        else 
            return 0;
    }
    
    int main(void)
    {
        int* ptr;
        int isAllocated = allocatingMemory(&ptr);
        if(isAllocated == 0)
        {
            *ptr = 999;
            free(ptr);
        }
    
        return 0;
    }
    

    If you are working with linked lists and say for example, you want to modify the head. You will pass a pointer to pointer (Note that, it is not called as double pointer) to head node.

    0 讨论(0)
  • 2021-01-16 05:38

    h is actually a copy of the original pointer, so your original pointer doesnot get modified. That is why you should use a double pointer.

    There are numerous questions related to that on SO. for example Using single versus double pointers in Linked lists implemented in C

    0 讨论(0)
  • 2021-01-16 05:43

    To change memory in the caller's context, a function needs to have a pointer to that memory.

    If the caller of your function has an empty list in a variable, and does an insert on that list like so:

    struct list *numbers = NULL;
    
    list_insert(numbers, 4711);
    

    then of course inside list_insert() all we have is the NULL pointer, so we can't change the value of the variable numbers in the caller's context.

    If, on the other hand, we're given a pointer to the variable, we can change the variable.

    That said, it's much cleaner (in my opinion) to return the new head of the list, i.e. make the function's signature be struct list * list_insert(struct list *head, int x);.

    0 讨论(0)
提交回复
热议问题