C语言编程中的字符串

发布于 2024-12-11 08:23:00 字数 147 浏览 0 评论 0原文

为什么我无法编译包含代码的程序

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

若相惜即相离 2024-12-18 08:23:00

这是因为您的代码片段不执行声明,而是赋值

char name[10];  // Declaration 

name= "Rajesh"; // Assignment.

并且数组在 C 中不能直接赋值。

名称 name 实际上解析为其第一个元素 (&name[0]) 的地址,该元素不是 左值,因此不能作为赋值的目标。

字符串变量声明和赋值

字符串变量可以像其他数组一样声明:

char phrase[14];

字符串数组可以同时初始化或部分初始化使用“{}”大括号括起来的值列表来声明时间(其他数据类型的数组也是如此)。例如,语句

char phrase[14] = {'E','n','t','e','r',' ','a','g','e',':',' ','\0'};

都声明了数组“phrase”并将其初始化为状态。该语句

char phrase[14] = "Enter age: ";

是等效的。如果省略“14”,将创建一个足够大的数组来包含值“”Enterage:“”和哨兵字符“'\0'”,以便这两个语句

char phrase[] = {'E','n','t','e','r',' ','a','g','e',':',' ','\0'};
char phrase[] = "Enter age: ";

彼此等效并且与语句等效

char phrase[12] = "Enter age: ";

但是,重要的是要记住字符串变量是数组,因此我们不能仅使用运算符“=”和“进行赋值和比较==。例如,我们不能简单地编写

phrase = "You typed: "; //Wrong way

相反,我们可以使用一组特殊的函数来进行字符串赋值和比较。

编辑:

>其他方法是使用指针来做到这一点:-

声明变量

char const *phrase;     /* a pointer to type character */

并在您想要的位置初始化变量,如

phrase = "Test string"; 

That's because your code snippet is not performing declaration, but assignment:

char name[10];  // Declaration 

name= "Rajesh"; // 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:

char phrase[14];

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

char phrase[14] = {'E','n','t','e','r',' ','a','g','e',':',' ','\0'};

both declares the array "phrase" and initialises it to the state. The statement

char phrase[14] = "Enter age: ";

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

char phrase[] = {'E','n','t','e','r',' ','a','g','e',':',' ','\0'};
char phrase[] = "Enter age: ";

are equivalent both to each other and to the statement

char phrase[12] = "Enter age: ";

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

phrase = "You typed: "; //Wrong way

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

char const *phrase;     /* a pointer to type character */

And initialize variable as where you want, as

phrase = "Test string"; 
别挽留 2024-12-18 08:23:00

您不能使用赋值来将值分配给字符串数组。
在C中,你只能初始化数组而不能分配它们,字符数组也不例外。

您将需要使用字符串复制函数,例如 strcpystrncpy 等。

但是,您可以将字符串封装在结构中并模拟:

typedef struct Yourstring Yourstring; 
struct Yourstring 
{ 
    char a[24]; 
};  
Yourstring a = { "abcd" }; 
Yourstring b = a; 
Yourstring c = { 0 }; 
c = b; 

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 or strncpy and so on.

However you can encapsulate a string in a struct and simulate this:

typedef struct Yourstring Yourstring; 
struct Yourstring 
{ 
    char a[24]; 
};  
Yourstring a = { "abcd" }; 
Yourstring b = a; 
Yourstring c = { 0 }; 
c = b; 
苍风燃霜 2024-12-18 08:23:00
char name[10];

在第一个示例中,您将 name 声明为一个包含 10 个字符的数组。符号 name 现在被解释为该数组的起始地址,但是虽然您可以写入数组,但无法移动符号 name.
因此,这

name= "Rajesh";

意味着将 name 指向远离您声明的数组的位置,并指向存储在内存中其他位置的字符串文字 "Rajesh" 的位置。你就是不能这样做。

您可以做的是:

strcpy(name, "Rajesh");

将字符串文字从可执行文件中的不可变位置复制到您声明的 char 数组中,或者:

char const *pointer_to_name = "Rajesh";

不复制任何内容,而仅存储您的地址将不可变字符串文字放入您可以使用的变量中,或者您的第二个示例:

char name[10]="Rajesh";

name 声明为 10 个字符的数组并对其进行初始化

char name[10];

In this first example, you're declaring name to be an array of ten characters. The symbol name is now interpreted as the starting address of this array, but while you can write into the array, you can't move the symbol name.
So, this:

name= "Rajesh";

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:

strcpy(name, "Rajesh");

which copies your string literal from it's immutable location in your executable, into the char array you declared, or:

char const *pointer_to_name = "Rajesh";

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:

char name[10]="Rajesh";

which declares name to be an array of 10 characters and initialises it.

梦行七里 2024-12-18 08:23:00
char name[10];
name= "Rajesh";

这里的name是一个字符数组。
简单的 name 基本上是指向数组第一个元素的指针,并且不能像上面的语句中那样为它分配一些值。

我的观点是

char name[10];
名称=“拉杰什”;

解释:
这不是正确的数组声明。字符串只是以“\0”运算符结尾的字符集合。因此,数组索引(在本例中为'name')基本上指向数组中第一个字符的地址,即name 保存'Rajesh' 中字符'R' 的地址。

输入图像描述这里

如果你想像上面提到的那样初始化,更好的方法可能是:

char name[10];
*名称=“拉杰什”;

现在,上面的声明不会抛出任何错误,但仍然会抛出警告,例如:

赋值从指针生成整数而不进行强制转换[-Wint-conversion]
*姓名=“拉杰什”

char name[10];
name= "Rajesh";

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'.

enter image description here

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"

稀香 2024-12-18 08:23:00

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.

猥︴琐丶欲为 2024-12-18 08:23:00
char name[10] ="Rajesh";

这是一个数组初始化。编译器知道这一点。这是一击必杀的伎俩。仅当定义变量时才能使用它。它相当于:

char name[10] = { 'R', 'a', 'j', 'e', 's', 'h', '\0' };

另一个是非法的,因为你不能在数组定义之外使用数组初始化

char name[10] ="Rajesh";

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:

char name[10] = { 'R', 'a', 'j', 'e', 's', 'h', '\0' };

The other one is illegal, because you can't use array initialization outside of array definition.

灯下孤影 2024-12-18 08:23:00

我不记得我在哪里读到它,但 C 标准说,你可以在定义处为数组分配一个字符串值,但不能在定义之后。

char a[10]="rajesh" its a defination hence works
char a[10];a="rajesh"; fails its not a defination

相反,如果不是定义,您需要使用 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.

char a[10]="rajesh" its a defination hence works
char a[10];a="rajesh"; fails its not a defination

rather you need to use strcpy(a,"rajesh") to assing a value for a string, if its not a defination

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文