简体   繁体   English

如何在c ++中使用NULL(或0)初始化静态字符数组

[英]how to initialize static char array with NULL(or 0) in c++

I attempted to initialize char array with NULL like this syntax.我试图像这种语法一样用 NULL 初始化 char 数组。

char str[5] = NULL;

But it returned error.. How can I initialize char array with NULL or 0 in C++?但它返回错误.. 如何在 C++ 中用 NULL 或 0 初始化字符数组?

Concretely, I want to print "Hello" in this example code.具体来说,我想在这个示例代码中打印“Hello”。

#include <iostream>
int main()
{
 char str[5] = NULL;
 if(!str)
  std::cout << "Hello" << std::endl;
 return 0;
}

This code will return error because of incorrect initialization.由于初始化不正确,此代码将返回错误。 Then, what initializing sentence should I replace sentence with?那么,我应该用什么初始化句子替换句子?

An array can not be null.数组不能为空。 Null is state of a pointer, and an array is not a pointer. Null 是指针的状态,数组不是指针。

In the expression !str , the array will decay to the address of the first element.在表达式!str ,数组将衰减到第一个元素的地址。 The type of this decayed address is a pointer, but you cannot modify the decayed pointer.这个衰减地址的类型是一个指针,但是你不能修改这个衰减的指针。 Since the array is never stored in the address 0 (except maybe in some special case on an embedded system where an array might actually be stored at that memory location), !str will never be true.由于数组永远不会存储在地址 0 中(除非在嵌入式系统上的某些特殊情况下,数组可能实际上存储在该内存位置),因此!str永远不会为真。

What you can do, is initialize all of the sub-objects of the array to zero.您可以做的是将数组的所有子对象初始化为零。 This can be achieved using the value-initialization syntax:这可以使用值初始化语法来实现:

char str[5]{};

As I explained earlier, !str is still not meaningful, and is always false.正如我之前解释过的, !str仍然没有意义,并且始终为 false。 One sensible thing that you might do is check whether the array contains an empty string.您可能会做的一件明智的事情是检查数组是否包含空字符串。 An empty string contains nothing except the terminator character (the array can have elements after the terminator, but those are not part of the string).空字符串除终止符外不包含任何内容(数组可以在终止符之后包含元素,但这些元素不是字符串的一部分)。

Simplest way to check whether a string is empty is to check whether the first character is the terminator (value-initialized character will be a terminator, so a value initialized character array does indeed contain the empty string):检查字符串是否为空的最简单方法是检查第一个字符是否为终止符(值初始化字符将是终止符,因此值初始化字符数组确实包含空字符串):

if (!str[0]) // true if the string is empty
//...
char str[5] = {'\0'};
if (str[0] != '\0')
//...

If you now put some characters into str it will print str up to the last '\\0'.如果您现在将一些字符放入 str 它将打印 str 直到最后一个 '\\0'。 Every string literal ends with '\\0', you must make sure your array ends with '\\0' too, if not, data will be read beyond your array (until '\\0' is encountered) and possibly beyond your application's memory space in which case your app will crash.每个字符串文字都以 '\\0' 结尾,你必须确保你的数组也以 '\\0' 结尾,否则,数据将被读取到你的数组之外(直到遇到 '\\0' )并且可能超出你的应用程序的内存空间在这种情况下,您的应用程序将崩溃。

You should use std::string or QString however if you need a string.但是如果你需要一个字符串,你应该使用 std::string 或 QString 。

You can't initialise a char array with NULL , arrays can never be NULL .您不能使用NULL初始化字符数组,数组永远不能为NULL You seem to be mixing up pointers and arrays.您似乎混淆了指针和数组。 A pointer could be initialised with NULL .指针可以用NULL初始化。

You can initialise the chars of your array with 0, that would be您可以使用 0 初始化数组的字符,即

char str[5] = {0};

but I don't think that's what you're asking.但我认为这不是你要问的。

#include <stdio.h>
using namespace std;

int main() {

char str[5];
for(int i=0;i<5;i++){
    str[i]=NULL;
}
printf("success");
return 0;
}

Hope this helps.希望这可以帮助。

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

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