简体   繁体   English

如何使用C中的char指针初始化char数组

[英]How to initialize a char array using a char pointer in C

Let's say I have a char pointer called string1 that points to the first character in the word "hahahaha". 假设我有一个名为string1的char指针指向单词“hahahaha”中的第一个字符。 I want to create a char[] that contains the same string that string1 points to. 我想创建一个包含string1指向的相同字符串的char []。

How come this does not work? 为什么这不起作用?

char string2[] = string1;

"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: 参考文献:

1) pointer string2 == pointer string1 1)指针string2 ==指针string1

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 其中一个的价值变化不会改变另一个

In C you have to reserve memory to hold a string. C中,您必须保留内存以保存字符串。
This is done automatically when you define a constant string, and then assign to a char[]. 这在定义常量字符串时自动完成,然后分配给char []。

On the other hand, when you write string2 = string1 , 另一方面,当你写string2 = string1
what you are actually doing is assigning the memory addresses of pointer-to-char objects. 你实际在做的是分配指向char对象的内存地址。 If string2 is declares as char* (pointer-to-char), then it is valid the assignment: 如果string2声明为char* (指向char*指针),那么赋值是有效的:

char* string2 = "Hello.";

The variable string2 now holds the address of the first character of the constanta array of char "Hello.". 变量string2现在保存char“Hello。”的constanta数组的第一个字符的地址。

It is fine, also, to write string2 = string1 when string2 is a char* and string1 is a char[] . string2char*string1char[]时,写string2 = string1也没关系。

However, it is supposed that a char[] has constant address in memory. 但是,假设char[]在内存中具有常量地址。 Is not modifiable. 不可修改。
So, it is not allowed to write sentences like that: 所以,不允许写这样的句子:

 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! */

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: 初始化程序在幕后执行此操作:

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. C是一种非常低级的语言。 Your statement: 你的陈述:

char str[] = another_str;

doesn't make sense to C. It is not possible to assign an entire array, to another in C. You have to copy letter by letter, either manually or using the strcpy() function. 对C没有意义。无法整个数组分配给 C中的另一个数组。您必须手动或使用strcpy()函数逐字复制。 In the above statement, the initializer does not know the length of the another_str array variable. 在上面的语句中,初始化程序不知道another_str数组变量的长度。 If you hard code the string instead of putting another_str , then it will work. 如果您对字符串进行硬编码而不是放置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. 你负责它。

You can assign the address of string1 to string2 using a pointer like this: 您可以使用如下指针将string1的地址分配给string2:

#include <stdio.h>

int main(int argc, char** argv) {
    char* string1 = "hahahahaha";
    char* string2 = string1;

    printf("%s\n", string2);
}

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

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