数组指针初始化的两种方法之间的区别

时间:2021-11-01 05:58:31

Please explain the difference between

请解释一下之间的区别

char* str = "Hello";

And

char* str = {"Hello"};

3 个解决方案

#1


ISO 9899-1990 6.5.7 ("Initialization") says :

ISO 9899-1990 6.5.7(“初始化”)说:

An array of character type may be initialized by a character string literal, optionally enclosed in braces.

字符类型数组可以由字符串文字初始化,可选地用大括号括起来。

#2


This one as I believe is a previously answered question. The link would be - Braces around string literal in char array declaration valid? (e.g. char s[] = {"Hello World"})

我认为这是一个以前回答的问题。链接将是 - 字符串文字中的字符串数字声明有效吗? (例如char s [] = {“Hello World”})

Both the declarations are the same. The answer to why it even existed is to provide some variety just to suit coders' tastes.(syntactic sugar) The only thing that I wanted to point out would be the char array variable declaration vs character pointer declaration. The pointers that you have defined have not been allocated any memory. Hence any edit/write operations on the string would lead to segmentation fault. Consider declaring

两个声明都是一样的。它为什么存在的答案就是提供一些变化以满足编码员的口味。(语法糖)我唯一要指出的是char数组变量声明与字符指针声明。您定义的指针尚未分配任何内存。因此,对字符串的任何编辑/写入操作都将导致分段错误。考虑申报

    char str[] = "Hello";

or

    char str[] = {"Hello"};

#3


There is no difference between those cases.

这些案件之间没有区别。

They both assign an address of string literal to a char pointer, but in second case an unusual syntax is used for it.

它们都将字符串文字的地址分配给char指针,但在第二种情况下,它使用了不常见的语法。

Similarly, int a = 42; and int a = {42}; are equivalent.

同样,int a = 42;和int a = {42};是等价的。


In the comments you've mentioned char *a = "Hello"; and char a[] = "Hello";.

在评论中你提到了char * a =“Hello”;和char a [] =“你好”;

They are completely different. Second one creates an array. It means same thing as

它们完全不同。第二个创建一个数组。它意味着同样的事情

char a[] = {'H','e','l','l','o','\0'};

There is no number inside [] becase compiler can guess array's size for you (6 in this case).

[]内部没有数字,因为编译器可以为您猜测数组的大小(在本例中为6)。

And another case is completely different.

另一个案例完全不同。

When you use a "string literal" outside of intialization of char array, like in this case

当你在char数组的初始化之外使用“字符串文字”时,就像在这种情况下一样

printf("Hello");

or

char *a = "Hello";

compiler implictly creates an array of const char to hold your string. As you know, in these contexts name of array decays to pointer to it's first element. So,

编译器隐含地创建一个const char数组来保存你的字符串。如您所知,在这些上下文中,数组的名称会衰减到指向它的第一个元素。所以,

char *a = "Hello";

and

const char internal_array[] = "Hello";

char *a = internal_array; // same as  char *a = &internal_array[0];

are equivalent.

If you try to do something like

如果你尝试做类似的事情

char *a = "Hello";
a[0] = 'A';

you will get a crash, because despite being a pointer to non-const char, a actually points to a constant string. Modifying it is not a good idea.

你会得到一个崩溃,因为尽管是一个指向非const char的指针,实际上它指向一个常量字符串。修改它不是一个好主意。


What about other case,

那么其他情况呢,

char a[] = "Hello";
a[0] = 'A';

is perfectly fine. In this case you get a new array of char that holds a string. Of course it's nonconst, so you can modify it.

很好。在这种情况下,您将获得一个包含字符串的新char数组。当然这是不可靠的,所以你可以修改它。

#1


ISO 9899-1990 6.5.7 ("Initialization") says :

ISO 9899-1990 6.5.7(“初始化”)说:

An array of character type may be initialized by a character string literal, optionally enclosed in braces.

字符类型数组可以由字符串文字初始化,可选地用大括号括起来。

#2


This one as I believe is a previously answered question. The link would be - Braces around string literal in char array declaration valid? (e.g. char s[] = {"Hello World"})

我认为这是一个以前回答的问题。链接将是 - 字符串文字中的字符串数字声明有效吗? (例如char s [] = {“Hello World”})

Both the declarations are the same. The answer to why it even existed is to provide some variety just to suit coders' tastes.(syntactic sugar) The only thing that I wanted to point out would be the char array variable declaration vs character pointer declaration. The pointers that you have defined have not been allocated any memory. Hence any edit/write operations on the string would lead to segmentation fault. Consider declaring

两个声明都是一样的。它为什么存在的答案就是提供一些变化以满足编码员的口味。(语法糖)我唯一要指出的是char数组变量声明与字符指针声明。您定义的指针尚未分配任何内存。因此,对字符串的任何编辑/写入操作都将导致分段错误。考虑申报

    char str[] = "Hello";

or

    char str[] = {"Hello"};

#3


There is no difference between those cases.

这些案件之间没有区别。

They both assign an address of string literal to a char pointer, but in second case an unusual syntax is used for it.

它们都将字符串文字的地址分配给char指针,但在第二种情况下,它使用了不常见的语法。

Similarly, int a = 42; and int a = {42}; are equivalent.

同样,int a = 42;和int a = {42};是等价的。


In the comments you've mentioned char *a = "Hello"; and char a[] = "Hello";.

在评论中你提到了char * a =“Hello”;和char a [] =“你好”;

They are completely different. Second one creates an array. It means same thing as

它们完全不同。第二个创建一个数组。它意味着同样的事情

char a[] = {'H','e','l','l','o','\0'};

There is no number inside [] becase compiler can guess array's size for you (6 in this case).

[]内部没有数字,因为编译器可以为您猜测数组的大小(在本例中为6)。

And another case is completely different.

另一个案例完全不同。

When you use a "string literal" outside of intialization of char array, like in this case

当你在char数组的初始化之外使用“字符串文字”时,就像在这种情况下一样

printf("Hello");

or

char *a = "Hello";

compiler implictly creates an array of const char to hold your string. As you know, in these contexts name of array decays to pointer to it's first element. So,

编译器隐含地创建一个const char数组来保存你的字符串。如您所知,在这些上下文中,数组的名称会衰减到指向它的第一个元素。所以,

char *a = "Hello";

and

const char internal_array[] = "Hello";

char *a = internal_array; // same as  char *a = &internal_array[0];

are equivalent.

If you try to do something like

如果你尝试做类似的事情

char *a = "Hello";
a[0] = 'A';

you will get a crash, because despite being a pointer to non-const char, a actually points to a constant string. Modifying it is not a good idea.

你会得到一个崩溃,因为尽管是一个指向非const char的指针,实际上它指向一个常量字符串。修改它不是一个好主意。


What about other case,

那么其他情况呢,

char a[] = "Hello";
a[0] = 'A';

is perfectly fine. In this case you get a new array of char that holds a string. Of course it's nonconst, so you can modify it.

很好。在这种情况下,您将获得一个包含字符串的新char数组。当然这是不可靠的,所以你可以修改它。