How do pointer to pointers work in C?

前端 未结 14 1248
渐次进展
渐次进展 2020-11-22 02:03

How do pointers to pointers work in C? When would you use them?

相关标签:
14条回答
  • 2020-11-22 02:42

    A pointer to pointer is, well, a pointer to pointer.

    A meaningfull example of someType** is a bidimensional array: you have one array, filled with pointers to other arrays, so when you write

    dpointer[5][6]

    you access at the array that contains pointers to other arrays in his 5th position, get the pointer (let fpointer his name) and then access the 6th element of the array referenced to that array (so, fpointer[6]).

    0 讨论(0)
  • 2020-11-22 02:44

    You may want to read this : Pointers to Pointers

    Hope this helps to clarify some basic doubts.

    0 讨论(0)
  • 2020-11-22 02:45

    Let's assume an 8 bit computer with 8 bit addresses (and thus only 256 bytes of memory). This is part of that memory (the numbers at the top are the addresses):

      54   55   56   57   58   59   60   61   62   63   64   65   66   67   68   69
    +----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+
    |    | 58 |    |    | 63 |    | 55 |    |    | h  | e  | l  | l  | o  | \0 |    |
    +----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+
    

    What you can see here, is that at address 63 the string "hello" starts. So in this case, if this is the only occurrence of "hello" in memory then,

    const char *c = "hello";
    

    ... defines c to be a pointer to the (read-only) string "hello", and thus contains the value 63. c must itself be stored somewhere: in the example above at location 58. Of course we can not only point to characters, but also to other pointers. E.g.:

    const char **cp = &c;
    

    Now cp points to c, that is, it contains the address of c (which is 58). We can go even further. Consider:

    const char ***cpp = &cp;
    

    Now cpp stores the address of cp. So it has value 55 (based on the example above), and you guessed it: it is itself stored at address 60.


    As to why one uses pointers to pointers:

    • The name of an array usually yields the address of its first element. So if the array contains elements of type t, a reference to the array has type t *. Now consider an array of arrays of type t: naturally a reference to this 2D array will have type (t *)* = t **, and is hence a pointer to a pointer.
    • Even though an array of strings sounds one-dimensional, it is in fact two-dimensional, since strings are character arrays. Hence: char **.
    • A function f will need to accept an argument of type t ** if it is to alter a variable of type t *.
    • Many other reasons that are too numerous to list here.
    0 讨论(0)
  • 2020-11-22 02:45

    There so many of the useful explanations, but I didnt found just a short description, so..

    Basically pointer is address of the variable. Short summary code:

         int a, *p_a;//declaration of normal variable and int pointer variable
         a = 56;     //simply assign value
         p_a = &a;   //save address of "a" to pointer variable
         *p_a = 15;  //override the value of the variable
    
    //print 0xfoo and 15 
    //- first is address, 2nd is value stored at this address (that is called dereference)
         printf("pointer p_a is having value %d and targeting at variable value %d", p_a, *p_a); 
    

    Also useful info can be found in topic What means reference and dereference

    And I am not so sure, when can be pointers useful, but in common it is necessary to use them when you are doing some manual/dynamic memory allocation- malloc, calloc, etc.

    So I hope it will also helps for clarify the problematic :)

    0 讨论(0)
  • 2020-11-22 02:56

    Consider the below figure and program to understand this concept better.

    As per the figure, ptr1 is a single pointer which is having address of variable num.

    ptr1 = #
    

    Similarly ptr2 is a pointer to pointer(double pointer) which is having the address of pointer ptr1.

    ptr2 = &ptr1;
    

    A pointer which points to another pointer is known as double pointer. In this example ptr2 is a double pointer.

    Values from above diagram :

    Address of variable num has : 1000
    Address of Pointer ptr1 is: 2000
    Address of Pointer ptr2 is: 3000
    

    Example:

    #include <stdio.h>
    
    int main ()
    {
       int  num = 10;
       int  *ptr1;
       int  **ptr2;
    
       // Take the address of var 
       ptr1 = &num;
    
       // Take the address of ptr1 using address of operator &
       ptr2 = &ptr1;
    
       // Print the value
       printf("Value of num = %d\n", num );
       printf("Value available at *ptr1 = %d\n", *ptr1 );
       printf("Value available at **ptr2 = %d\n", **ptr2);
    }
    

    Output:

    Value of num = 10
    Value available at *ptr1 = 10
    Value available at **ptr2 = 10
    
    0 讨论(0)
  • 2020-11-22 02:58

    How do pointers to pointers work in C?

    First a pointer is a variable, like any other variable, but that holds the address of a variable.

    A pointer to a pointer is a variable, like any other variable, but that holds the address of a variable. That variable just happens to be a pointer.

    When would you use them?

    You can use them when you need to return a pointer to some memory on the heap, but not using the return value.

    Example:

    int getValueOf5(int *p)
    {
      *p = 5;
      return 1;//success
    }
    
    int get1024HeapMemory(int **p)
    {
      *p = malloc(1024);
      if(*p == 0)
        return -1;//error
      else 
        return 0;//success
    }
    

    And you call it like this:

    int x;
    getValueOf5(&x);//I want to fill the int varaible, so I pass it's address in
    //At this point x holds 5
    
    int *p;    
    get1024HeapMemory(&p);//I want to fill the int* variable, so I pass it's address in
    //At this point p holds a memory address where 1024 bytes of memory is allocated on the heap
    

    There are other uses too, like the main() argument of every C program has a pointer to a pointer for argv, where each element holds an array of chars that are the command line options. You must be careful though when you use pointers of pointers to point to 2 dimensional arrays, it's better to use a pointer to a 2 dimensional array instead.

    Why it's dangerous?

    void test()
    {
      double **a;
      int i1 = sizeof(a[0]);//i1 == 4 == sizeof(double*)
    
      double matrix[ROWS][COLUMNS];
      int i2 = sizeof(matrix[0]);//i2 == 240 == COLUMNS * sizeof(double)
    }
    

    Here is an example of a pointer to a 2 dimensional array done properly:

    int (*myPointerTo2DimArray)[ROWS][COLUMNS]
    

    You can't use a pointer to a 2 dimensional array though if you want to support a variable number of elements for the ROWS and COLUMNS. But when you know before hand you would use a 2 dimensional array.

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