C ++字符串:[] vs. * [英] C++ strings: [] vs. *
问题描述
一直在想,用[]或*声明变量有什么区别?我看到的方式:
Been thinking, what's the difference between declaring a variable with [] or * ? The way I see it:
char *str = new char[100];
char str2[] = "Hi world!";
..应该是主要的区别,虽然我不确定如果你可以做
.. should be the main difference, though Im unsure if you can do something like
char *str = "Hi all";
..因为指针应该引用一个静态成员,我不知道是否可以吗?
.. since the pointer should the reference to a static member, which I don't know if it can?
无论如何,真正令我难过的是知道之间的区别:
Anyways, what's really bugging me is knowing the difference between:
void upperCaseString(char *_str) {};
void upperCaseString(char _str[]) {};
那么,如果有人能告诉我差异,
So, would be much appreciated if anyone could tell me the difference? I have a hunch that both might be compiled down the same, except in some special cases?
Ty
推荐答案
让我们来看看它(对于下面,注意 char const
和 const char
在C ++中相同):
Let's look into it (for the following, note char const
and const char
are the same in C++):
hello
是一个包含6个常量字符的数组: char const [6]
。作为每个数组,它可以隐式转换为指向其第一个元素的指针: char const * s =hello;
为了与C代码兼容,C ++允许一个其他转换,这将是其他形式不成立: char * s =hello;
它删除const!这是一个异常,允许C-ish代码编译,但不建议使 char *
指向字符串文字。那么我们对 char * s =foo;
?
"hello"
is an array of 6 const characters: char const[6]
. As every array, it can convert implicitly to a pointer to its first element: char const * s = "hello";
For compatibility with C code, C++ allows one other conversion, which would be otherwise ill-formed: char * s = "hello";
it removes the const!. This is an exception, to allow that C-ish code to compile, but it is deprecated to make a char *
point to a string literal. So what do we have for char * s = "foo";
?
foo
- > 数组到指针
- > char const *
- > code> qualification-conversion - > char *
。字符串文字是只读的,不会在堆栈上分配。你可以自由地使指针指向它们,并从函数中返回那个,而不会崩溃:)。
"foo"
-> array-to-pointer
-> char const*
-> qualification-conversion
-> char *
. A string literal is read-only, and won't be allocated on the stack. You can freely make a pointer point to them, and return that one from a function, without crashing :).
现在, char [] =hello;
?这是一个整体其他的东西。这将创建一个字符数组,并填充String hello
。字面不指向。相反,它被复制到字符数组。并且在堆栈上创建数组 。您无法从函数有效地返回指向它的指针。
Now, what is char s[] = "hello";
? It's a whole other thing. That will create an array of characters, and fill it with the String "hello"
. The literal isn't pointed to. Instead it is copied to the character-array. And the array is created on the stack. You cannot validly return a pointer to it from a function.
如何让你的函数接受一个数组作为参数?您只需将参数声明为数组:
How can you make your function accept an array as parameter? You just declare your parameter to be an array:
void accept_array(char foo[]);
但您省略了大小。实际上,任何大小都会这样做,因为它被忽略:标准说,以这种方式声明的参数将转换为与
but you omit the size. Actually, any size would do it, as it is just ignored: The Standard says that parameters declared in that way will be transformed to be the same as
void accept_array(char * foo);
游览:多维数组
char
以任何类型,包括数组本身:
Excursion: Multi Dimensional Arrays
Substitute char
by any type, including arrays itself:
void accept_array(char foo[][10]);
接受一个二维数组,其最后一个维的大小为10. 多维数组是下一维的第一个子数组!现在,让我们转换它。它将是一个指向它的第一个元素的指针。所以,实际上它将接受一个指向10个字符的数组的指针:(删除头中的 []
,然后只是创建一个指针指向您在头中看到的类型然后):
accepts a two-dimensional array, whose last dimension has size 10. The first element of a multi-dimensional array is its first sub-array of the next dimension! Now, let's transform it. It will be a pointer to its first element again. So, actually it will accept a pointer to an array of 10 chars: (remove the []
in head, and then just make a pointer to the type you see in your head then):
void accept_array(char (*foo)[10]);
由于数组隐式转换为指向其第一个元素的指针,因此您只需传递一个二维数组(它的最后一个维度大小是10),它会工作。事实上,对于任何 n维数组都是这种情况,包括 n = 1
的特殊情况;
As arrays implicitly convert to a pointer to their first element, you can just pass an two-dimensional array in it (whose last dimension size is 10), and it will work. Indeed, that's the case for any n-dimensional array, including the special-case of n = 1
;
void upperCaseString(char *_str) {};
和
void upperCaseString(char _str[]) {};
是相同的,因为第一个只是一个指向char的指针。但是请注意,如果你想传递一个字符串文字(假定它不改变它的参数),那么你应该更改参数为 char const * _str
不要做弃用的东西。
are the same, as the first is just a pointer to char. But note if you want to pass a String-literal to that (say it doesn't change its argument), then you should change the parameter to char const* _str
so you don't do deprecated things.
这篇关于C ++字符串:[] vs. *的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!