Why we used double and triple pointer in objective-C or C language?

前端 未结 5 484
误落风尘
误落风尘 2021-01-14 17:14

I confused when i want to take single pointer and when should i take double pointer?
In following structure what exactly did?



        
相关标签:
5条回答
  • 2021-01-14 17:51

    It's probably not the case in the code that you referenced, but you also need a double pointer any time you want to pass a pointer to a function and have changes to that pointer be reflected outside the scope of that function.

    For example, if you were trying to rewrite the strcpy function so that the user did not have to allocate memory for the source string, you might try something like the following:

    void MyStrcpy(char* dst, char* src){ 
      dst = (char*)malloc(sizeof(char)*(strlen(src)+1));
      for(int i=0;i<=strlen(src);i++)
        dst[i] = src[i];
      printf("src: %s ", src);
      printf("dst: %s\n\n", dst);
    }
    

    If you were then to call that function,

    int main() {
         char *foo = "foo";
         char *newPtr;
    
         MyStrcpy(newPtr, foo);
    
         printf("foo: %s ", foo);
         printf("new: %s\n", newPtr);
    }
    

    your output would be as follows:

    src: foo dst: foo

    foo: foo new:

    You might also get a seg fault when trying to print newPtr, depending your system. The reason for this behavior is the exact same as the reason you wouldn't expect a change to an int that was passed by value to a function to be reflected outside of that function: what you are passing to MyStrcpy is simply the memory address that newPtr references. When you malloc the space for dst inside the function, you are changing the address dst points to. This change will not be reflected outside of the scope of MyStrcpy!

    Instead, if you wanted newPtr to point to the new allocated chunk of memory, you need to have dst be a pointer to a pointer, a char **.

    void MyStrcpy(char** dst, char* src){ 
      *dst = (char*)malloc(sizeof(char)*(strlen(src)+1));
      for(int i=0;i<=strlen(src);i++)
        (*dst)[i] = src[i];
      printf("src: %s ", src);
      printf("dst: %s\n\n", *dst);
    }
    

    Now, if you were to call that function:

    int main() {
      char *foo = "foo";
      char *newPtr;
    
      MyStrcpy(&newPtr, foo);
    
      printf("foo: %s ", foo);
      printf("new: %s\n", newPtr);
    }
    

    You would get your expected output:

    src: foo dst: foo

    foo: foo new: foo

    Hope that helps!

    0 讨论(0)
  • 2021-01-14 17:57

    See also these questions:

    • What is double star?
    • Why does NSError need double indirection? (pointer to a pointer)
    0 讨论(0)
  • 2021-01-14 17:57

    In general pointer is used to hold the address of another variable. What if we need to hold the address of pointer ,in that case we use double pointer. When we want to hold the address of double pointer we use triple pointer.

    0 讨论(0)
  • 2021-01-14 17:59

    Well, in C at least, double-pointers are commonly used for 2D arrays. The most common 2D array is probably an array of C strings (char*'s). Double pointers are also sometimes employed to pass pointers to functions by reference, but this is unlikely to be the use in the code sample you posted.

    According to the name methodLists I would guess that this is an array of lists. A (linked) list in C is commonly represented by a pointer to a node, which objc_method_list could be. An array of such lists is then implemented with a double pointer.

    0 讨论(0)
  • 2021-01-14 18:09

    In the most general case a double pointer is a pointer to a list of pointers.

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