Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/meteor/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 使用strlen的结果作为字符的长度*_C++_Arrays_Constants_Declaration_String Literals - Fatal编程技术网

C++ 使用strlen的结果作为字符的长度*

C++ 使用strlen的结果作为字符的长度*,c++,arrays,constants,declaration,string-literals,C++,Arrays,Constants,Declaration,String Literals,我的意图是根据我已经创建的字符*的长度创建一个字符[] 我问自己为什么这是无效的: void copyStringsV2() { const char* source = "this is my string."; const int length = strlen(source); const char* dest[length]; } 编译器给了我以下提示: Severity Code Description Project Fil

我的意图是根据我已经创建的字符*的长度创建一个字符[]

我问自己为什么这是无效的:

void copyStringsV2() {
    const char* source = "this is my string.";
    const int length = strlen(source);

    const char* dest[length];
}
编译器给了我以下提示:

Severity    Code    Description Project File    Line    Suppression State
Warning C4101   'str_b': unreferenced local variable    CStrings     
xxx\cstrings.cpp    46  
Error   C2131   expression did not evaluate to a constant   CStrings     
xxx\cstrings.cpp    161 
Error (active)  E0028   expression must have a constant value   CStrings     
xxx\CStrings.cpp    161 
expression did not evaluate to a constant
你能帮我吗

为什么这是不可编译的

因为这是C++,C++提供了各种各样的工具。您最好的选择是std::string。可以复制和传递字符串,而无需编写额外的代码

#include <string>

int main()
{
    const std::string source = "this is my string.";
    const std::string dest = source;
    // do something with dest
}

1,所以不需要,这不是C++的一部分。

< p>您试图声明一个可变长度数组,固定长度数组的长度必须在编译时已知。这就是编译器错误所抱怨的

如果直到运行时才知道长度,则必须动态分配复制的字符串,例如通过new[]:

无效复制字符串v2 { const char*source=这是我的字符串。; const int length=strlensource; char*dest=新字符[长度+1]; strcpydest,来源; ... 删除[]dest; } 或者std::vector,因此不需要直接使用new[]/delete[]:

包括 无效复制字符串v2 { const char*source=这是我的字符串。; const int length=strlensource; 标准::向量长度+1; strcpydest.data,来源; ... } 但是,在这种情况下,最好使用std::string:

包括 无效复制字符串v2 { const char*source=这是我的字符串。; std::string dest=源; ... }
对于初学者,在这个声明中应该使用类型size\u t而不是类型int

const int length = strlen(source);
      ^^^
const char* dest[length];
此常量是运行时的常量。所以你不能在声明中使用它

const int length = strlen(source);
      ^^^
const char* dest[length];

因为这里声明了一个可变长度数组和可变长度数组,VLA不是标准C++特性。 另外,不清楚为什么数组的元素类型是const char*而不是const char

此外,应初始化一个常量对象

我的意图是创建一个char[]

这不是一个字符数组。这是指向const char的指针数组

此外,如果您希望数组能够适应原始的空终止字符串,则必须在数组长度中包含空终止符,以便其大小为长度+1

我问自己为什么这是无效的:

void copyStringsV2() {
    const char* source = "this is my string.";
    const int length = strlen(source);

    const char* dest[length];
}
编译器给了我以下提示:

Severity    Code    Description Project File    Line    Suppression State
Warning C4101   'str_b': unreferenced local variable    CStrings     
xxx\cstrings.cpp    46  
Error   C2131   expression did not evaluate to a constant   CStrings     
xxx\cstrings.cpp    161 
Error (active)  E0028   expression must have a constant value   CStrings     
xxx\CStrings.cpp    161 
expression did not evaluate to a constant
你能帮我吗

数组的大小必须是编译时常量。长度不是常量,因此不能用作数组的长度

不能在编译时通过指向字符串元素的指针计算字符串的长度。但是,如果使用了引用,并且使用了constexpr函数来获取长度,并且使用了constexpr const works too变量,则可以将其用作数组的大小。C++标准库中有这样的函数:

auto& source = "this is my string.";
constexpr auto length = std::char_traits<char>::length(source);
char dest[length + 1];

看看你的实际目标是什么?因为这个代码是错误的。无论你真正想做什么,都有更好的方法。问题是,请把问题集中在实际问题上,而不是你对这个问题的解决方案上。编译器不够聪明,没有意识到const char*source=这是我的字符串。;不会在运行时更改,因此const int length=strlensource;在运行时不会改变,因此长度可能是一个编译时常量,可用作数组维度。@user4581301谢谢你,伙计,我刚刚想到strlen在运行时会成为现实:谢谢你的分享,我不想好奇,但为什么我应该使用size\u t而不是int?这能帮助我度过最初的日子吗?为什么D@SirRollalotsize_t是函数strlen的返回类型。一般来说,int类型的对象不能大到足以容纳size\u t类型的所有值。这意味着strlen在运行时使用,不是吗?@SirRollalot就抽象机器而言,是的。尽管优化程序可能会执行编译时魔法,但这不是字符数组。这是指向const char的指针数组。这是否意味着我为指针分配1/2,为字符分配1/2-@SirRollalot我不明白你的问题。@eeroika这是否意味着当我创建一个指针数组时,我可以用另一种方式保存内存?谢谢分享,这将是我书的下一章。我只想在我变得懒惰之前学习ALS基本的东西:DThis不是懒惰,这是C++。如果你想要无用的并发症,试试cobol。如果你想要有用的并发症,呵呵,在接下来的章节里你会很开心的!我只是想在我学会如何处理那些让我的程序员生活更轻松的类之前,先学会这些东西。来自爪哇,我已经有几年的懒惰了:dbuut。。。这意味着const int length=strlensource;在运行时进行计算。如果我错了,请纠正我!是的,strlen仅在运行时计算。如果需要编译时评估,则必须使用类似于以下内容的工具 nstead:const char source[]=这是我的字符串。;const int length=std::sizesource-1;或者@eerorika向您展示的char_traits::length解决方案。