How does an array of pointers to pointers work?

2020-02-08 06:20发布

char **Data[70]={NULL};  

What is the correct terminology for this? How else could it be written? What does it look like in memory? I am reading many tutorials on pointers but I don't see it in this syntax. Any help is appreciated. Thanks.

5条回答
劫难
2楼-- · 2020-02-08 06:57

This isn't very obvious:

char **Data[70]={NULL};

but with an alternative declaration, like:

char* Data[2][3] = {
  {"Nick", "Tom", "Helen"},
  {"one", "two", "three"}
};

we can easily see that it's a 2 dimensional array of strings.

Edit: I used Data[2][3] to show that it's a 2D array. I used fixed size for the dimensions like 2 & 3 just for demonstration. Of course we can have:

char* Data[][3]={
  {"Nick", "Tom", "Helen"},
  {"one", "two", "three"},
  // ...
};

or char** Data[]

Ok, here is what I mean by 2-D array:

char** Data[2]={0};

void test()
{
  char* d1[] = {"1", "2"};
  char* d2[] = {"10", "20", "30"};
  Data[0] = d1;
  Data[1] = d2;

  printf("%s\n", Data[0][0]);
  printf("%s\n", Data[0][1]);
  printf("%s\n", Data[1][0]);
  printf("%s\n", Data[1][1]);
  printf("%s\n", Data[1][2]);
}
查看更多
走好不送
3楼-- · 2020-02-08 07:09

This structure

char **Data[70]={NULL};

is an array of 70 pointers to pointers to char. The compiler allocates 70 * sizeof(char**) bytes for this array, which assuming 32-bit pointers is 280 bytes.

If you internally think of a "pointer to char" as a string, which isn't true but it's close enough, then this is an array of 70 pointers to strings. To make some ASCII art and pretend that you have allocated and filled some values....

 Array of        One or more
 char **           char *
+---------+     +---------+
|    0    | --> |   ptr   | -->  "Hello, world"
+---------+     +---------+
|    1    |
+---------+       +---------+
|    2    | ----> |  ptr2   | -->  "Goodbye, cruel world"
+---------+       +---------+
|    3    |
+---------+         +---------+
|    4    | ------> | ptr3[0] | -->  "Message 0"
+---------+         +---------+
    ...             | ptr3[1] | -->  "Message 1"
+---------+         +---------+
|   69    |         | ptr3[2] | -->  "Message 2"
+---------+         +---------+

You could do the above with code like this (error checking malloc return values skipped):

char **Data[70]={NULL};
char **ptr, **ptr2, **ptr3;

ptr = (char **) malloc(sizeof(char *));
*ptr = "Hello, world";
Data[0] = ptr;

ptr2 = (char **) malloc(sizeof(char *));
*ptr2 = "Goodbye, cruel world";
Data[2] = ptr2;

ptr3 = (char **) malloc(10 * sizeof(char *));
Data[4] = ptr3;

ptr3[0] = "Message 0";
ptr3[1] = "Message 1";
 ...
ptr3[9] = "Message 9"; 

printf("%s\n", *Data[0]);
printf("%s\n", Data[2][0]);
printf("%s\n", Data[4][0]);
printf("%s\n", Data[4][1]);
      ...
printf("%s\n", Data[4][9]);

Think of it this way: Each entry in the array is a char **. Each entry can point to an arbitrary location in memory, said location(s) being char * and thus being able to point to a null-terminated character array aka "string."

Note carefully the distinction between this and what you get when you allocate a 2D array:

char *Data2[10][70]={NULL};

The allocation of Data2 above gives you a 2-dimensional array of char * pointers, said 2-d array being allocated in a single chunk of memory (10 * 70 * sizeof(char*) bytes, or 2800 bytes with 32-bit pointers). You don't have the ability to assign the char ** pointers to arbitrary locations in memory that you have with the single-dimensional array of char ** pointers.

Also note (given above declarations of Data and Data2) that the compiler will generate different code for the following array references:

Data[0][0]
Data2[0][0]

Here's another way to think about this: Imagine that you have several arrays of pointers to strings:

char *table0[] = { "Tree", "Bench", "Stream" };
char *table1[] = { "Cow", "Dog", "Cat" };
char *table2[] = { "Banana", "Carrot", "Broccoli" };
char **Data[3];

Data[0] = table0;
Data[1] = table1;
Data[2] = table2;

You have an array of pointers to "array of pointer to char". If you now print the value of data[1][1], think of it like this: data[1] gets you a pointer to the array table1. Then the value table1[1] equals "Dog".

查看更多
家丑人穷心不美
4楼-- · 2020-02-08 07:11

It's slightly tricky to think of a practical use for an array of char**. Especially one with 70 elements.

However, suppose that I'm going to run 70 programs. As you probably know, program arguments are commonly passed as a char** argv parameter to main() (or char*[] argv, which in a function signature is the same thing). So if I wanted to store the argv pointers for all those programs, I'd used an array just like Data. Obviously I'd also need some more memory elsewhere, for the actual strings and argv arrays to occupy, but it's a start.

Initializing an array with {NULL} sets all its elements to NULL. This is a useful shorthand: you can initialize an array with {firstelement, secondelement, ...}, but if you don't provide enough terms, all the rest are treated as 0.

Just like any other array of pointers, initialized with {NULL}, what this looks like in memory is 70 NULL pointers sitting in a row. There's (usually) no difference in memory between a char** and any other object pointer. I think it is legal to write a weird implementation in which there is a difference, but don't hold your breath waiting to come across one.

So, the difference between 70 NULL char** in a row and 70 NULL char* in a row is what would be on the other end of the pointer, if they weren't NULL. On the other end of a char** is a pointer to a char. On the other end of a char* is a char. Either the pointer, or the char, might be the first in an array, depending on how it's being used.

查看更多
贼婆χ
5楼-- · 2020-02-08 07:14

This is, effectively, a pointer to a pointer to pointers. However, since a "pointer" is nothing but a location in memory, there really isn't much use in doing this over just doing char* Data[70], other than making it obvious that each char* is a pointer to another char*, instead of a pointer to char.

查看更多
聊天终结者
6楼-- · 2020-02-08 07:22

What you've got is an array of 70 pointers, each of which points to another pointer, each of those pointers point to a char. On an interesting note, arrays themselves are pointers so you have three levels of pointers.

查看更多
登录 后发表回答