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

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

问题描述

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

什么是这个正确的术语?否则怎么可以把它写?是什么样子的记忆?我读指针上很多教程,但我没有看到它在这个语法。任何帮助是AP preciated。谢谢你。

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.

推荐答案

此结构

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

是70指针的指针为char数组。编译器分配 70 * sizeof的(字符**)字节数组,其中假设32位指针为280个字节。

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.

如果您在内部认为字符指针作为一个字符串,这是不正确的,但它足够接近,那么这是70指针数组的字符串。为了使一些ASCII艺术并且已分配并填充一些值pretend ....

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

您可以做code这样上面的(错误检查malloc的返回值跳过):

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]);

想想这样说:数组中的每一项都是一个的char ** 。每个条目可以指向任意位置在内存中,所说的位置(S)为的char * ,因而能够指向一个空结束的字符数组又名弦。

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};

数据2 上面为您提供了的char *的2维数组指针的分配表示,2-D数组被分配在一个单独的内存块( 10 * 70 * sizeof的(字符*)字节或32位指针2800字节)。您不必分配字符的能力** 指向内存中你有炭的单维数组的任意位置* * 指针。

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.

另外请注意(上述数据的声明给数据2 ),编译器会产生不同的code以下数组引用:

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;

您有一个指针数组的指针数组为char。如果现在打印的值数据[1] [1] ,认为它是这样的:数据[1] 让你一个指向数组表1 。那么该值表1 [1] 等于

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天全站免登陆