C语言编程中的字符串
为什么我无法编译包含代码的程序
char name[10];
name= "Rajesh";
虽然我能够编译程序
char name[10]="Rajesh";
why am i unable to compile the program containing the code
char name[10];
name= "Rajesh";
While i am able to compile a program with
char name[10]="Rajesh";
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
这是因为您的代码片段不执行声明,而是赋值:
并且数组在 C 中不能直接赋值。
名称
name
实际上解析为其第一个元素 (&name[0]
) 的地址,该元素不是 左值,因此不能作为赋值的目标。字符串变量声明和赋值
字符串变量可以像其他数组一样声明:
字符串数组可以同时初始化或部分初始化使用“{}”大括号括起来的值列表来声明时间(其他数据类型的数组也是如此)。例如,语句
都声明了数组“phrase”并将其初始化为状态。该语句
是等效的。如果省略“14”,将创建一个足够大的数组来包含值“”Enterage:“”和哨兵字符“'\0'”,以便这两个语句
彼此等效并且与语句等效
但是,重要的是要记住字符串变量是数组,因此我们不能仅使用运算符“=”和“进行赋值和比较==。例如,我们不能简单地编写
相反,我们可以使用一组特殊的函数来进行字符串赋值和比较。
编辑:
>其他方法是使用指针来做到这一点:-
声明变量
并在您想要的位置初始化变量,如
That's because your code snippet is not performing declaration, but assignment:
And arrays are not directly assignable in C.
The name
name
actually resolves to the address of its first element (&name[0]
), which is not an lvalue, and as such cannot be the target of an assignment.String Variable Declarations and Assignments
String variables can be declared just like other arrays:
String arrays can be initialised or partially initialised at the same time as being declared, using a list of values enclosed in "{}" braces (the same is true of arrays of other data types). For example, the statement
both declares the array "phrase" and initialises it to the state. The statement
is equivalent. If the "14" is omitted, an array will be created just large enough to contain both the value ""Enter age: "" and the sentinel character "'\0'", so that the two statements
are equivalent both to each other and to the statement
However, it is important to remember that string variables are arrays, so we cannot just make assignments and comparisons using the operators "=" and "==". We cannot, for example, simply write
Instead, we can use a special set of functions for string assignment and comparison.
Edited :
And other way is to do that, using pointer : -
Declare variable
And initialize variable as where you want, as
您不能使用赋值来将值分配给字符串数组。
在C中,你只能初始化数组而不能分配它们,字符数组也不例外。
您将需要使用字符串复制函数,例如
strcpy
或strncpy
等。但是,您可以将字符串封装在结构中并模拟:
You cannot assign values to string arrays by using assignment.
In C, You can only initialize arrays not assign them, a array of characters is no exception for this rule.
You will need to use string copying functions like
strcpy
orstrncpy
and so on.However you can encapsulate a string in a struct and simulate this:
在第一个示例中,您将
name
声明为一个包含 10 个字符的数组。符号name
现在被解释为该数组的起始地址,但是虽然您可以写入数组,但无法移动符号name.
因此,这
意味着将
name
指向远离您声明的数组的位置,并指向存储在内存中其他位置的字符串文字"Rajesh"
的位置。你就是不能这样做。您可以做的是:
将字符串文字从可执行文件中的不可变位置复制到您声明的 char 数组中,或者:
不复制任何内容,而仅存储您的地址将不可变字符串文字放入您可以使用的变量中,或者您的第二个示例:
将
name
声明为 10 个字符的数组并对其进行初始化。In this first example, you're declaring
name
to be an array of ten characters. The symbolname
is now interpreted as the starting address of this array, but while you can write into the array, you can't move the symbolname
.So, this:
would mean pointing
name
away from the array you declared and at the location of the string literal"Rajesh"
which is stored elsewhere in memory. You just can't do this.What you can do is either:
which copies your string literal from it's immutable location in your executable, into the char array you declared, or:
which doesn't copy anything, but merely stores the address of your immutable string literal into a variable where you can use it, or your second example:
which declares
name
to be an array of 10 characters and initialises it.这里的
name
是一个字符数组。简单的
name
基本上是指向数组第一个元素的指针,并且不能像上面的语句中那样为它分配一些值。我的观点是
char name[10];
名称=“拉杰什”;
解释:
这不是正确的数组声明。字符串只是以“\0”运算符结尾的字符集合。因此,数组索引(在本例中为'name')基本上指向数组中第一个字符的地址,即name 保存'Rajesh' 中字符'R' 的地址。
如果你想像上面提到的那样初始化,更好的方法可能是:
char name[10];
*名称=“拉杰什”;
现在,上面的声明不会抛出任何错误,但仍然会抛出警告,例如:
赋值从指针生成整数而不进行强制转换[-Wint-conversion]
*姓名=“拉杰什”
Here
name
is an array of characters.The simple
name
is basically pointer to first element of array and it cannot be assigned with some value like it's done in above statement.My point is
char name[10];
name= "Rajesh";
Explanation:
This is not the correct declaration of array. Strings are nothing but collection of characters terminated with '\0' operator. So, the array index (which in this case is 'name', basically points to address of 1st character in array i.e name holds the address of character 'R' in 'Rajesh'.
If you want to initialize like as mentioned above, the better approach could have been:
char name[10];
*name= "Rajesh";
Now, the above declaration won't throw any error, but still it will throw warning, like:
assignment makes integer from pointer without a cast [-Wint-conversion]
*name = "Rajesh"
AS
char name[10]="Rajesh"
是定义编译器理解您想要做什么并纠正您的错误。在 C++ 中,用 "" 编写的字符串是常量,一些编译器将它们放入字符串池中以节省空间。name="...."
意味着您试图将常量分配给非常量指针,这是不允许的。您应该使用 strcpy 将字符串复制到数组中。
AS
char name[10]="Rajesh"
is definition compiler understands what are you trying to do and corrects your mistake. In c++ strings written in "" are constant and some compilers put them to stringpools to save space.name="...."
means that you are trying to assign a constant to a non-constant pointer which is not allowed.you should use strcpy to copy a string into an array.
这是一个数组初始化。编译器知道这一点。这是一击必杀的伎俩。仅当定义变量时才能使用它。它相当于:
另一个是非法的,因为你不能在数组定义之外使用数组初始化。
This one is an array initialization. The compiler knows of it. It's a one-shot trick. You can use it only when you define your variable. It would be equivalent to:
The other one is illegal, because you can't use array initialization outside of array definition.
我不记得我在哪里读到它,但 C 标准说,你可以在定义处为数组分配一个字符串值,但不能在定义之后。
相反,如果不是定义,您需要使用 strcpy(a,"rajesh") 来为字符串赋值
I dont remember where exactly i read it but C standard says, you can assign a string value for an array at the defination but not after the defination.
rather you need to use strcpy(a,"rajesh") to assing a value for a string, if its not a defination