简体   繁体   English

C ++:为什么不能使用初始化程序char foo []将字符串转换为C字符串?

[英]C++: Why can't I convert a string to C-string with an initializer char foo[]?

This is my code: 这是我的代码:

const char readArr[] = readWord.c_str(); 

This gives an error: array initializer must be an initializer list or string literal 这给出了一个错误:数组初始化程序必须是初始化程序列表或字符串文字

Why must I use 我为什么要用

const char *readArr = readWord.c_str();?

It's for the same reason you can't 出于同样的原因,你不能

const char *p="foo";

const char readArr[]=p;

either. 要么。 An array is not a discrete object that can be initialized. 数组不是可以初始化的离散对象。 The only thing that can be initialized in C++ is a discrete object, and an array is not an object per se. 在C ++中唯一可以初始化的是离散对象,而数组本身并不是对象。 An array is a conceptual address of a contiguous list of values, in consecutive memory locations. 数组是连续存储位置中的连续值列表的概念性地址。 When an array is used in an expression, such as: 在表达式中使用数组时,例如:

readArr[i]

The array's name decays to a pointer to the first element in the array. 数组的名称衰减为指向数组第一个元素的指针。 Now, guess what you did when you wrote this: 现在,猜猜您写这篇文章时做了什么:

const char *readArr = readWord.c_str();

Well, you just stored a pointer to the first element in an array of characters, that's owned by the readWord std::string . 好吧,您只是将指向第一个元素的指针存储在一个字符数组中,该数组由readWord std::string

In a regular array declaration: 在常规数组声明中:

char readArr[]="Hello";

the compiler is given the length of the string, and thus it initialize a consecutive list of character values, and the label readArr to it. 编译器被赋予了字符串的长度,因此它初始化了一个连续的字符值列表,并readArr加上了标签readArr

const char readArr[] = readWord.c_str();

The reason this is not legal is that it simply doesn't make sense to initialise an array from a pointer. 之所以不合法,是因为从指针初始化数组根本没有意义。 A pointer is in essence a memory address: it points to some data, whether that data is dynamically or statically allocated (allocated 'on the heap' or 'on the stack' respectively). 指针本质上是一个内存地址:它指向一些数据,无论该数据是动态分配还是静态分配(分别在“堆上”或“堆上”分配)。 A pointer does not record how much memory is there. 指针不记录那里有多少内存。

This is confusing to newcomers to C and C++ because the language often allows you to treat arrays as if they were just pointers to their first element. 这会使C和C ++的新手感到困惑,因为该语言通常允许您将数组视为只是指向其第一个元素的指针。 That doesn't mean that arrays are just pointers to their first element. 这并不意味着数组只是指向他们的第一个元素。 They aren't. 他们不是。 But if you use them in an expression they will decay to a pointer to their first element. 但是,如果在表达式中使用它们,它们将衰减为指向其第一个元素的指针。

Because arrays are not pointers. 因为数组不是指针。 An array... is an array, period. 数组...是一个数组,句点。 char readArr[] (just like char arr[4] ) declares something directly in the local memory space (the stack, for a function) so that something has to be statically allocated. char readArr[] (就像char arr[4] )直接在本地内存空间(函数的堆栈)中声明某些内容,因此必须静态分配某些内容。

str.c_str() is somewhere on the heap so that can't work. str.c_str()在堆上的某个位置,因此无法正常工作。

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

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