C语言 如何在C中使用char指针初始化char数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18413934/
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
How to initialize a char array using a char pointer in C
提问by user2481095
Let's say I have a char pointer called string1 that points to the first character in the word "hahahaha". I want to create a char[] that contains the same string that string1 points to.
假设我有一个名为 string1 的字符指针,它指向单词“hahahaha”中的第一个字符。我想创建一个 char[] 包含与 string1 指向的相同的字符串。
How come this does not work?
这怎么行不通?
char string2[] = string1;
回答by Mitch Wheat
"How come this does not work?"
“这怎么行不通?”
Because that's not how the C language was defined.
因为这不是 C 语言的定义方式。
You can create a copy using strdup()[Note that strdup()is not ANSI C]
您可以使用strdup()[注意这strdup()不是 ANSI C]创建副本
Refs:
参考:
回答by Aaron Gong
1) pointer string2 == pointer string1
1) 指针字符串 2 == 指针字符串 1
change in value of either will change the other
任何一个值的变化都会改变另一个
From poster poida
来自海报poida
char string1[] = "hahahahaha";
char* string2 = string1;
2) Make a Copy
2) 复印
char string1[] = "hahahahaha";
char string2[11]; /* allocate sufficient memory plus null character */
strcpy(string2, string1);
change in value of one of them will not change the other
其中一个值的变化不会改变另一个
回答by pablo1977
In Cyou have to reserve memory to hold a string.
This is done automatically when you define a constant string, and then assign to a char[].
在C 中,您必须保留内存来保存字符串。
当您定义一个常量字符串,然后分配给一个 char[] 时,这会自动完成。
On the other hand, when you write string2 = string1,
what you are actually doing is assigning the memory addresses of pointer-to-charobjects. If string2is declares as char*(pointer-to-char), then it is valid the assignment:
另一方面,当您编写 时string2 = string1,
您实际所做的是分配指向字符对象的指针的内存地址。如果string2声明为char*(指向字符的指针),则赋值有效:
char* string2 = "Hello.";
The variable string2now holds the address of the first character of the constanta array of char "Hello.".
该变量string2现在保存了 char "Hello." 的常量数组的第一个字符的地址。
It is fine, also, to write string2 = string1 when string2is a char*and string1is a char[].
也可以在string2is achar*和string1is a时写 string2 = string1 char[]。
However, it is supposed that a char[]has constant address in memory. Is not modifiable.
So, it is not allowed to write sentences like that:
但是,假设 achar[]在内存中具有恒定地址。不可修改。
所以,不允许写这样的句子:
char string2[];
string2 = (something...);
However, you are able to modify the individual characters of string2, because is an array of characters:
但是,您可以修改 string2 的单个字符,因为它是一个字符数组:
string2[0] = 'x'; /* That's ok! */
回答by Naveen
What you write like this:
你这样写:
char str[] = "hello";
... actually becomes this:
......实际上变成了这样:
char str[] = {'h', 'e', 'l', 'l', 'o'};
Here we are implicitly invoking something called the initializer.
Initializer is responsible for making the character array, in the above scenario.
Initializer does this, behind the scene:
这里我们隐式调用了一个叫做初始化器的东西。
Initializer 负责制作字符数组,在上面的场景中。
初始化程序在幕后执行此操作:
char str[5];
str[0] = 'h';
str[1] = 'e';
str[2] = 'l';
str[3] = 'l';
str[4] = 'o';
C is a very low level language. Your statement:
C是一种非常低级的语言。你的声明:
char str[] = another_str;
doesn't make sense to C.
It is not possible to assignan entire array, to another in C. You have to copy letter by letter, either manually or using the strcpy() function.
In the above statement, the initializer does not know the length of the another_strarray variable. If you hard code the string instead of putting another_str, then it will work.
对 C 没有意义。在 C 中不可能将整个数组分配给另一个数组。您必须逐个字母复制,手动或使用 strcpy() 函数。在上面的语句中,初始化器不知道another_str数组变量的长度。如果您对字符串进行硬编码而不是放置another_str,那么它将起作用。
Some other languages might allow to do such things... but you can't expect a manual car to switch gears automatically. You are in charge of it.
其他一些语言可能允许做这样的事情……但你不能指望手动汽车自动换档。你负责。

