什么是创建在C字符串方式之间的区别? [英] What is the difference between the ways to create a string in C?

查看:95
本文介绍了什么是创建在C字符串方式之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是C语言中这两种形式的字符串变量之间的区别?

What is the difference between these two forms of a string variable in C language?

char *string1;
char string2[];

有没有其他办法做到这一点?

Is there any other way to do it?

非常感谢你。

推荐答案

的char *字符串1 =富;

字符串1是一个指向一个字符串(为参数的缘故,它指向存储在程序的只读数据段的一系列字符)。

string1 is a pointer to a string literal (for the sake of argument, it points to a series of characters stored in a read-only data segment of the program).

字符字符串2 [] =富;

string2为4个字符数组。它被初始化的字节'F','O','O',ASCII_NUL。

string2 is an array of 4 characters. It is initialised with the bytes 'f', 'o', 'o', ASCII_NUL.

也许最显著的区别是,如果你做字符串1 [0] ='B'; 你得到了一个未定义的行为,因为你想修改存储重presentation一个字符串。如果你这样做字符串2 [0] ='B'; 则修改您的个人字符串为嘘,这是很好的。

Probably the most significant difference is that if you do string1[0] = 'b'; you get undefined behaviour, because you're trying to modify the stored representation a string literal. If you do string2[0] = 'b'; then you modify your personal string to "boo", which is fine.

在一般情况下,键入的char * 的变量是一个指针为char。它经常被用来指向字符的NULL结尾的序列中的第一个字符,在这种情况下,它指向一个字符串。类型的变量的char [] 是字符数组。如果它有一个NUL终止,那么它实际上是一个字符串。

In general, a variable of type char* is a pointer to a char. It's often used to point to the first char in a NUL-terminated sequence of chars, in which case it points to a string. A variable of type char[] is an array of chars. If it has a NUL terminator, then it actually is a string.

问题是轻微两个事实感到困惑:

The issue is slightly confused by two facts:

1)在C中,每当一个数组变量名中采用一个指针一个上下文中使用,它是指一个指针数组的第一个元素。所以数组和指针经常被认为是可以互换的。

1) In C, whenever an array variable name is used in a context that takes a pointer, it "means" a pointer to the first element of the array. So arrays and pointers are often thought to be interchangeable.

2)在C中,键入的char [] 的函数参数的的实际上是一个数组。这只是一个指针,完全一样的的char * 。所以,再次,数组和指针经常被认为是可以互换的。

2) In C, a function parameter of type char[] is not in fact an array. It's just a pointer, exactly the same as char*. So, again, arrays and pointers are often thought to be interchangeable.

所以,指针和阵列之间的另一个区别是:

So, another difference between the pointer and the array:

string1 = "bar"; // changes string1 to point to another string literal.

string1 = string2; // changes string1 to point to the first character of string2.

string2 = string1; // doesn't compile - you can't assign to an array,
                   //   only initialize it and then modify element-by-element.

[注:报关字符字符串2 []; 中的问题不是在功能有效C语法,但我已经使用的定义将是有效无论是在一个函数或在文件范围内,任何函数之外。无论哪种方式,他们的行为,因为我已经为初始化和分配说明,但它们有不同的生命周期。]

[Note: the declaration char string2[]; in the question is not valid C syntax in a function, but the definitions I've used would be valid either in a function or at file scope, outside any function. Either way they behave as I've described for initialization and assignment, but they have different lifetimes.]

这篇关于什么是创建在C字符串方式之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆