简体   繁体   English

char **是指向C中字符串数组的指针吗?

[英]Is char** a pointer to an array of strings in C?

From my understanding, char* X is a variable that points to a single character or a character array (string) in C. 据我了解, char* X是一个变量,它指向C中的单个字符或字符数组(字符串)。

char** is a pointer which points to another pointer which finally points to a single character or a character array. char**是一个指向另一个指针的指针,该指针最终指向单个字符或字符数组。

if int** is equivalent to creating a multidimensional array, why can't I create an array of strings in C using char** ? 如果int**等效于创建多维数组,为什么我不能使用char**在C中创建字符串数组?

const char** day = {
    "Sunday",
    "Monday",
    "Tuesday",
    "Wednesday",
    "Thursday",
    "Friday",
    "Saturday"
};

Here *day would point to the array itself and **day would point to the first element of the array "Sunday"? 这里的*day将指向数组本身,而**day将指向数组“ Sunday”的第一个元素?

why can't I create an array of strings in C using char**? 为什么我不能使用char **在C中创建字符串数组? yes you can create. 是的,您可以创建。 Correct way is to use array of char pointers not the double char** pointer as you did. 正确的方法是使用char指针数组,而不是像以前那样使用double char**指针。 For eg 例如

const char* day[7] = {
    "Sunday",
    "Monday",
    "Tuesday",
    "Wednesday",
    "Thursday",
    "Friday",
    "Saturday"
};

Above, day is array of char pointers and its each element is character pointer and each elements needs to point to valid address. 上面, day字符指针数组,其每个元素都是字符指针 ,每个元素都需要指向有效地址。 Here day[0] is character pointer & its pointing to string literal sunday which is valid address. 这里day[0]是字符指针,它指向字符串文字sunday ,即有效地址。

Its also possible via char** but not the way as you did. 也可以通过char**但不能像您那样进行。 For eg 例如

char **day = {"sunday", "Monday" };

here problem is you haven't allocated memory for day[0] , day[1] to hold some string literal like sunday & so on. 这里的问题是您没有为day[0]day[1]分配内存来保存一些字符串文字,例如sunday等等。

First allocate memory for day like below 首先为如下所示的day分配内存

char **day = malloc(NUM_OF_DAYS * sizeof(*day)); /* define NUM_OF_DAYS as 7 */

And then allocate memory for day[0] , day[1] etc. 然后为day[0]day[1]等分配内存。

for(int index =0 ; index < NUM_OF_DAYS; index++) {
   day[index] = malloc(MAX_DAY_SIZE * sizeof(**day)); /* define this MACRO */
}

And then you scan the data at run time. 然后在运行时扫描数据。

char* X is a variable that points to a single character or a character array (string) in C. char* X是一个变量,它指向C中的单个字符或字符数组(字符串)。

No, it is a variable that contains the address of a single character. 不,它是一个包含单个字符地址的变量。 That single character may or may not be the beginning of a null terminated string. 该单个字符可能是也可能不是以null结尾的字符串的开头。

Thus char** points at a char* that points at a character. 因此, char**指向char*char*

if int** is equivalent to creating a multidimensional array 如果int**等效于创建多维数组

It is not. 它不是。 int** has nothing to do with multi-dimensional arrays. int**与多维数组无关。

why can't I create an array of strings in C using char** ? 为什么我不能使用char**在C中创建字符串数组?

Because a char** is not an array nor can it, strictly speaking, point at one. 因为char**不是数组,严格来说,它也不能指向一个数组。


A char* can be used to point at a null terminated string. char*可用于指向以空终止的字符串。 So if you want an array of strings, you need an array of char* . 因此,如果您需要一个字符串数组,则需要一个char*数组。 Which can for example be written as: 例如,可以将其写为:

char* day[] = { ... };

Now as it turns out, a char** can be used to point at the address of the first item of this array, &day[0] , since that is a char* . 事实证明,现在可以使用char**指向此数组的第一项 &day[0] ,因为它是char* This often make people confuse char** for multi-dimensional arrays, because it is valid to do this: 这通常会使人们混淆char**用于多维数组,因为这样做是有效的:

char* day[] = { "Sunday", ... };
char** week = &day[0];
printf("%c", week[0][0]); // prints 'S'

But that does not make char** an array, nor does it make it a 2D array. 但这不会使char**成为数组,也不会使其成为2D数组。

You can also use char** to dynamically allocate a look-up table of strings, where each string has a variable length: 您还可以使用char**动态分配字符串查找表,其中每个字符串的长度都可变:

char** week = malloc(7 * sizeof(char*));
for(int i=0; i<7; i++)
{
  week[i] = malloc( ... );
}

Here again, the char** points at the first item of a 1D array of char* . 同样, char**指向一维char*数组的第一项。 It is not a 2D array, it does not point at one. 不是 2D数组,也不指向1。 That it allows week[i][j] syntax is irrelevant, it is still not an array (and [] is never actually used on array types, Do pointers support “array style indexing”? ). 它允许week[i][j]语法无关紧要,它仍然不是数组(并且[]从未在数组类型上实际使用, 指针是否支持“数组样式索引”? )。

More info: Correctly allocating multi-dimensional arrays . 更多信息: 正确分配多维数组

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM