简体   繁体   English

在C中将字符串分配给字符指针的说明

[英]Explanation for assigning a string to a character pointer in C

I am very much confused with pointers. 我对指针非常困惑。 If we create a pointer variable of type 'char', that is some thing like char *ch; 如果我们创建一个'char'类型的指针变量,那就像char *ch; and assign a character array to it. 并为其分配一个字符数组。 Then we can access each letter in the string by increasing the pointer (++ch). 然后,我们可以通过增加指针(++ ch)来访问字符串中的每个字母。

char *ch;
char a[5]="hello";
*ch=a;
while(*ch) {
  printf("%c",*ch);
  ++ch;
}

This works perfectly fine. 这工作得很好。 But I see many examples like, 但是我看到很多例子,

struct example {
  char *ch;
};

int main() {
  struct example ex={"hello"};
  printf("%s",ex.ch);
}

I am pretty confused with this example like how directly assigning a string to a character pointer(instead of assigning the string to a variable and then assigning it to character pointer) can make it accessible. 我对这个示例非常困惑,例如如何直接将字符串分配给字符指针(而不是将字符串分配给变量,然后将其分配给字符指针)如何使其可访问。 In the previous case, *ch points to the starting memory of the array "a". 在前一种情况下,* ch指向数组“ a”的起始存储器。 But in the second case to which memory does the pointer ch point to? 但是在第二种情况下,指针ch指向哪个内存? Can anyone give me a clear explanation? 谁能给我清楚的解释?

When you make this assignment 进行此作业时

ch=a;

you are setting the pointer ch to the beginning of the character array a[] (note: the fact that the array remains null-terminated is a pure coincidence - your code has undefined behavior, because the sixth character '\\0' at the end of "hello" is not copied into a five-character array). 您正在将指针ch设置为字符数组a[]的开头(请注意:数组保持为空终止的事实纯属巧合-您的代码具有未定义的行为,因为结尾处的第六个字符'\\0' "hello"的副本不会复制到5个字符的数组中)。 After the assignment ch becomes an "alias" to the C string stored inside the array a[] , which means that you can use it with string manipulation and formatting routines. 赋值ch成为存储在数组a[]的C字符串的“别名”之后,这意味着您可以将其与字符串操作和格式化例程一起使用。

Your second example is not an assignment, it is initialization: 您的第二个示例不是赋值,而是初始化:

struct example ex={"hello"};

Enclosed in curly braces is an initializer. 花括号中包含一个初始化程序。 C compiler will take each element (you've got only one, but there can be many) and assign it to the corresponding field of the struct . C编译器将采用每个元素(只有一个,但是可以有很多),并将其分配给struct的相应字段。 In this case, string literal "hello" is assigned to the ch member. 在这种情况下,字符串文字"hello"被分配给ch成员。

The rule for initializers is to assign the values in curly braces in declaration order. 初始化程序的规则是按声明顺序在大括号中分配值。 You have an opportunity to override it if you wish by using the designated initializer feature of C99: 如果愿意,可以使用C99的指定初始化器功能覆盖它:

struct example ex={ .ch = "hello"};

This code snippet 此代码段

char *ch;
char a[5]="hello";
*ch=a;
while(*ch)
{
printf("%c",*ch);
++ch;

}

has undefined behaviour. 具有不确定的行为。 That it would work fine you have to declare the character array either as 它将正常工作,您必须将字符数组声明为

char a[6]="hello";

or char a[]="hello"; 或char a [] =“ hello”;

In this case the array indeed will have the terminating zero. 在这种情况下,数组确实将具有终止的零。

As for this code snippet 至于此代码段

struct example
{
char *ch;
};
int main()
{
struct example ex={"hello"};
printf("%s",ex.ch);
}

then in this statement 然后在这句话

struct example ex={"hello"};

the compiler at first places string literal "hello" in memory as a character array having type char[6] and then assign pointer to the first element of the array to data member ch of the structure. 编译器首先将字符串文字“ hello”作为具有char[6]类型的字符数组放置在内存中,然后将指向数组第一个元素的指针分配给结构的数据成员ch。

The difference between these two declarations 这两个声明之间的区别

char ch[] = "hello";

and

char *ch = "hello";

is that that in the first case a character array is created each element of which is initialized by corresponding character of the string literal that itself will not be stored in memory. 是在第一种情况下,创建了一个字符数组,其每个元素都由字符串文字的相应字符初始化,该字符串本身不会存储在内存中。 In the second case the string literal will be stored in memory and pointer ch will contain the address of its first character. 在第二种情况下,字符串文字将存储在内存中,指针ch将包含其第一个字符的地址。

char a[] = "hi";
char *ch = "hello";

Here a is a char array of size 3 bytes to store, ' h ', ' i ' and ' \\0 '. 这里a是一个大小为3个字节的char数组,用于存储' h ',' i '和' \\0 '。 3 bytes of this memory is allocated in stack to keep the string. 该内存的3个字节在堆栈中分配,以保留字符串。

Here ch is a pointer variable, which store a address of char, and size of ch is 4 bytes to store the address (size of pointer is compiler specific). 此处ch是一个指针变量,它存储一个char地址,而ch大小是4个字节来存储该地址(指针的大小是编译器特定的)。 Then " hello " is a constant string literal which will be in text segment of process memory as a read only data. 然后“ hello ”是一个常量字符串文字,它将作为只读数据出现在过程存储器的文本段中。 And the address of the first byte is assigned to ch variable. 并且第一个字节的地址分配给ch变量。 4 byte of memory is allocated for ch variable to store address in stack and 5 byte of memory is required in text segment to keep the string literal " hello ". ch变量分配了4个字节的内存以将地址存储在堆栈中,并且在文本段中需要5个字节的内存以保持字符串文字“ hello ”。

For explanation in some other views read here . 有关其他观点的解释,请阅读此处

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

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