C++ 字符串:[] 与 *

时间:2023-04-24
本文介绍了C++ 字符串:[] 与 *的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

一直在想,用 [] 或 * 声明变量有什么区别?我的看法:

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?

推荐答案

我们来看一下(以下注意char constconst 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 语言代码,但不推荐使用 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" -> array-to-pointer -> char const* -> 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 s[] = "hello"; ?这是整体另一回事.这将创建一个字符数组,并用字符串 "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,包括数组本身:

void accept_array(char foo[][10]);

接受一个二维数组,其最后一维的大小为10.多维数组的第一个元素是其下一维的第一个子数组!现在,让我们改造它.它将再次成为指向其第一个元素的指针.因此,实际上它会接受一个指向 10 个字符的数组的指针:(删除 head 中的 [],然后只创建一个指向您在脑海中看到的类型的指针):

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++ 字符串:[] 与 *的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

上一篇:指向属于类成员的函数 - GLFW setKeycallback 下一篇:声明指向多维数组的指针并分配数组

相关文章

最新文章