开发者

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

I confused when i want to take single pointer and 开发者_如何学JAVAwhen should i take double pointer?

In following structure what exactly did?

struct objc_class {
    Class isa;
    Class super_class;
    const char *name;
    long version;
    long info;
    long instance_size;
    struct objc_ivar_list *ivars;
    struct objc_method_list **methodLists;
    struct objc_cache *cache;
    struct objc_protocol_list *protocols;
};

Why we use the methodLists double pointer?

Edited

int sqlite3_get_table(
      sqlite3 *db,         
      const char *zSql,    
      char ***pazResult,   
      int *pnRow,          
      int *pnColumn,       
      char **pzErrmsg      
    );

In above scenario what will be meaning of triple pointer char ***pazResult?


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.


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!


See also these questions:

  • What is double star?
  • Why does NSError need double indirection? (pointer to a pointer)


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


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜