C语言 将 C 中的字符串初始化为空字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4142745/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Initialize a string in C to empty string
提问by maayan
I want to initialize string in C to empty string. I tried:
我想将 C 中的字符串初始化为空字符串。我试过:
string[0] = "";
but it wrote
但它写道
"warning: assignment makes integer from pointer without a cast"
How should I do it then?
那我该怎么做呢?
采纳答案by badgerr
Assuming your array called 'string' already exists, try
假设您的名为“string”的数组已经存在,请尝试
string[0] = 'char myString[10];
myString[0] = 'char myString[10] = "";
';
';
\0is the explicit NUL terminator, required to mark the end of string.
\0是显式 NUL 终止符,用于标记字符串的结尾。
回答by Will Dean
You want to set the first character of the string to zero, like this:
您想将字符串的第一个字符设置为零,如下所示:
char s[10] = {'char s[10];
memset(s, 'char s[10];
strncpy(s, "", sizeof(s));
', sizeof(s));
'};
(Or myString[0] = 0;)
(或myString[0] = 0;)
Or, actually, on initialisation, you can do:
或者,实际上,在初始化时,您可以执行以下操作:
char string[] = "";
But that's not a general way to set a string to zero length once it's been defined.
但这不是在定义字符串后将字符串设置为零长度的通用方法。
回答by Matt Joiner
In addition to Will Dean's version, the following are common for whole buffer initialization:
除了 Will Dean 的版本之外,以下是整个缓冲区初始化的常见版本:
char str1[] = "";
char str2[5] = "";
printf("%d, %d\n", sizeof(str1), sizeof(str2)); //prints 1, 5
or
或者
char *string = NULL;
string = (char*)calloc(1, sizeof(char));
or
或者
char *string = NULL;
int numberOfChars = 50; // you can use as many as you need
string = (char*)calloc(numberOfChars + 1, sizeof(char));
回答by Amarghosh
Assigning string literals to char array is allowed only during declaration:
仅在声明期间才允许将字符串文字分配给 char 数组:
strcpy(string, "");
This declares string as a char array of size 1 and initializes it with \0.
这将 string 声明为大小为 1 的 char 数组并使用\0.
Try this too:
也试试这个:
string[0] = "";
回答by Vasilii P
callocallocates the requested memory and returns a pointer to it. It also sets allocated memory to zero.
calloc分配请求的内存并返回指向它的指针。它还将分配的内存设置为零。
In case you are planning to use your stringas empty string all the time:
如果您打算一直使用stringas 空字符串:
"warning: assignment makes integer from pointer without a cast
In case you are planning to store some value in your stringlater:
如果您打算在string以后存储一些价值:
//this will create an empty string without no memory allocation.
char str[]="";// it is look like {0}
回答by Konkret
To achieve this you can use:
为此,您可以使用:
// this is better if you know your string size.
char str[5]=""; // it is look like {0, 0, 0, 0, 0}
回答by pmg
Ok, let's dive into the expression ...
好的,让我们深入研究表达式......
0an int: represents the number of chars (assuming stringis (or decayed into) a char*) to advance from the beginning of the object string
0an int:表示string从对象的开头前进的字符数(假设是(或衰减为)一个字符*)string
string[0]: the charobject located at the beginning of the object string
string[0]: 位于char对象开头的对象string
"": string literal: an object of type char[1]
"":字符串文字:类型的对象 char[1]
=: assignment operator: tries to assign a value of type char[1]to an object of type char. char[1](decayed to char*) and charare not assignment compatible, but the compiler trusts you (the programmer) and goes ahead with the assignment anyway by casting the type char*(what char[1]decayed to) to an int--- and you get the warning as a bonus. You have a really nice compiler:-)
=:赋值运算符:尝试将类型的值分配给类型char[1]的对象char。char[1](衰减为char*)并且与char赋值不兼容,但是编译器信任您(程序员)并且无论如何都会通过将类型char*(char[1]衰减为)转换为int---来继续进行赋值,并且您会收到警告作为奖励。你有一个非常好的编译器:-)
回答by naseer mohammad
I think Amarghosh answered correctly. If you want to Initialize an empty string(without knowing the size) the best way is:
我认为 Amarghosh 回答正确。如果你想初始化一个空字符串(不知道大小),最好的方法是:
##代码##But if you want initialize a string with a fixed memory allocation you can do:
但是如果你想用固定的内存分配初始化一个字符串,你可以这样做:
##代码##回答by todhunter
It's a bit late but I think your issue may be that you've created a zero-length array, rather than an array of length 1.
有点晚了,但我认为您的问题可能是您创建了一个长度为零的数组,而不是长度为 1 的数组。
A string is a series of characters followed by a string terminator ('\0'). An empty string ("") consists of no characters followed by a single string terminator character - i.e. one character in total.
字符串是一系列字符,后跟字符串终止符 ( '\0')。空字符串 ( "") 不包含任何字符,后跟单个字符串终止符 - 即总共一个字符。
So I would try the following:
所以我会尝试以下方法:
string[1] = ""
string[1] = ""
Note that this behaviour is not the emulated by strlen, which does not count the terminator as part of the string length.
请注意,此行为不是由 模拟的strlen,它不会将终止符计为字符串长度的一部分。

