gpt4 book ai didi

c++ - C++ 中的空终止数组

转载 作者:太空宇宙 更新时间:2023-11-04 14:37:19 26 4
gpt4 key购买 nike

我想在构造函数中创建以 NULL 结尾的数组。

class Test
{
char name [30];
char Address[100]
};

Test::Test()
{
memset(name ,0, 30);
memset(Address, 0, 100);
}

这是将数组初始化为 NULL 的正确方法吗?

有什么好的替代方案吗?

最佳答案

如果您打算使用 C 风格的字符串,您只需将第一个字符设置为空终止符。

name[0] = Address[0] = 0;

但是,从长远来看,您最好改用 std::string

关于c++ - C++ 中的空终止数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8566518/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com