指向指针的指针数组如何工作? [英] How does an array of pointers to pointers work?

查看:37
本文介绍了指向指针的指针数组如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

对此的正确术语是什么?还能怎么写?它在记忆中是什么样子的?我正在阅读许多关于指针的教程,但我没有在这种语法中看到它.任何帮助表示赞赏.谢谢.

解决方案

这种结构

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

是一个包含 70 个指向 char 指针的指针的数组.编译器为此数组分配 70 * sizeof(char**) 字节,假设 32 位指针为 280 字节.

如果您在内部将指向 char 的指针"视为一个字符串,这不是真的,但它已经足够接近了,那么这是一个包含 70 个指向字符串的指针的数组.制作一些 ASCII 艺术并假装您已经分配并填充了一些值....

 一个或多个数组字符 ** 字符 *+---------+ +---------+|0 |-->|点 |-->你好,世界"+---------+ +---------+|1 |+---------+ +---------+|2 |---->|ptr2 |-->《再见,残酷的世界》+---------+ +---------+|3 |+---------+ +---------+|4 |------>|ptr3[0] |-->消息 0"+---------+ +---------+... |ptr3[1] |-->消息 1"+---------+ +---------+|69 ||ptr3[2] |-->消息2"+---------+ +---------+

你可以用这样的代码来做上面的事情(错误检查 malloc 返回值被跳过):

char **Data[70]={NULL};字符 **ptr, **ptr2, **ptr3;ptr = (char **) malloc(sizeof(char *));*ptr = "你好,世界";数据[0] = ptr;ptr2 = (char **) malloc(sizeof(char *));*ptr2 = "再见,残酷的世界";数据[2] = ptr2;ptr3 = (char **) malloc(10 * sizeof(char *));数据[4] = ptr3;ptr3[0] = "消息 0";ptr3[1] = "消息 1";...ptr3[9] = "消息 9";printf("%s\n", *Data[0]);printf("%s\n", 数据[2][0]);printf("%s\n", 数据[4][0]);printf("%s\n", 数据[4][1]);...printf("%s\n", 数据[4][9]);

这样想:数组中的每个条目都是一个char **.每个条目都可以指向内存中的任意位置,所述位置是 char *,因此能够指向以空字符结尾的字符数组,也就是字符串".

仔细注意这与分配二维数组时得到的区别:

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

Data2 上面的分配给你一个 char * 指针的二维数组,表示二维数组被分配在单个内存块中(10 * 70 * sizeof(char*) 字节,或 2800 字节,带 32 位指针).您无法将 char ** 指针分配到使用 char ** 指针的一维数组所拥有的内存中的任意位置.

还要注意(上面给出了 DataData2 的声明)编译器将为以下数组引用生成不同的代码:

数据[0][0]数据2[0][0]

这是另一种思考方式:假设您有几个指向字符串的指针数组:

char *table0[] = { "Tree", "Bench", "Stream" };char *table1[] = { "Cow", "Dog", "Cat" };char *table2[] = { "香蕉", "胡萝卜", "西兰花" };字符**数据[3];数据[0] = table0;数据[1] = 表1;数据[2] = table2;

您有一个指向char 指针数组"的指针数组.如果你现在打印 data[1][1] 的值,可以这样想:data[1] 得到一个指向数组 table1 的指针.那么值 table1[1] 等于 "Dog".

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.

解决方案

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".

这篇关于指向指针的指针数组如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆