简体   繁体   English

这4项之间有什么区别:字符,数组,字符串,文字。 在C?

[英]what's the difference between these 4 item:character,array,string,literal. in C?

there's some basic concepts that I get confused when reading a C book recently. 最近在阅读C书时,我感到很困惑。

It says: A variable that points to a string literal can't be used to change the contents of the string. 它说:指向字符串文字的变量不能用于更改字符串的内容。

As I know there's also character literal and integer literal , how's their situation? 据我所知,还有字符文字整数字面 ,他们的情况如何? Are they also can not be update? 他们也无法更新吗? If so can you give an example? 如果是这样,你能举个例子吗?

Besides, what's the difference between literal and array ? 另外, 文字数组有什么区别? like character array , string literal , are they actually one thing? 字符数组字符串文字 ,它们实际上是一回事吗?

what should I call the variable below? 我该怎么称呼下面的变量? an integer array ? 整数数组 an integer literal ? 整数字面量

int contestants[] = {1, 2, 3};

I've concluded some examples but I'm still somewhat messed: 我已经总结了一些例子,但我仍然有点混乱:

char s[] = "How big is it?";  //s is an array variable

char *t = s;  //t is a pointer variable to a string literal "How big is it?"

string literal:"ABC" 字符串文字:“ABC”

Character literal:'a' 字符文字:'a'

Integer literal:1 整数字面数:1

I'm messed by these 4 item: character,array,string,literal . 我被这4个项目搞乱了: 字符,数组,字符串,文字 I'm still very messed up. 我还是很乱。

character array and string literal same thing? 字符数组字符串文字同样的事情?

an array of characters and array literal same ? 一个字符 数组数组文字相同?

A literal is a token in a program text that denotes a value. 文字是程序文本中表示值的标记。 There are string literals like "123" , character literals like 'a' and numeric literals like 7 . 有像"123"这样的字符串文字,像'a'这样'a'字符文字和像7这样的数字文字。

int contestants[] = {1, 2, 3};

In the program fragment above there are three literals 1 2 and 3 and no others. 在上面的程序片段中,有三个文字1 23 ,没有其他文字。 In particular, neither contestants nor {1, 2, 3} are literals. 特别是, contestants{1, 2, 3}都不是文字。

It is worth noting that the C standard uses the word literal only in reference to string literals. 值得注意的是,C标准仅在引用字符串文字时使用单词literal。 The other kinds are officially known as constants. 其他种类正式称为常数。 But you may find them referred to as literals in all kind of places so I have included them here. 但是你可能会发现它们在所有类型的地方被称为文字,所以我把它们包含在这里。 "Integer literal" and "integer constant" are the same thing. “整数文字”和“整数常数”是一回事。

A string literal is also an object (a piece of data, a region of storage) in a program that is associated with a string literal in the previous sense. 字符串文字也是程序中的对象(一段数据,一个存储区域),该对象与先前意义上的字符串文字相关联。 This piece of data is a character array. 这段数据是一个字符数组。 Not every character array is a string literal. 并非每个字符数组都是字符串文字。 No int array is a literal. 没有int数组是文字。

A pointer can point to a string literal, but not to a character literal or to an integer literal, because the latter two kinds are not objects (have no storage associated with them). 指针可以指向字符串文字,但不能指向字符文字或整数文字,因为后两种不是对象(没有与之关联的存储)。 A pointer can only point to an object. 指针只能指向一个对象。 You cannot point a pounter to a literal 5. So the question of whether such things can be modified does not arise. 你不能将一个pounter指向一个字面5.所以不会出现这些事情是否可以修改的问题。

char* p = "123";

In a program fragment above, "123" is a literal and p points to it. 在上面的程序片段中, "123"是文字, p指向它。 You cannot modify an object pointed to by p . 您无法修改p指向的对象。

char a[] = "123";

In the program fragment above, a is a character array. 在上面的程序片段中, a是一个字符数组。 It is initialized with a string literal "123", but it is not a literal itself and can be modified freely. 它用字符串文字“123”初始化,但它本身不是文字,可以自由修改。

int i = 5;

Above, 5 is a literal and i is not. 以上, 5是文字而i不是。 i is initialized with a literal, but it isn't one itself. i用文字初始化,但它本身不是一个。

int k[] = {1, 2, 3};
int* kp = k;

In the line above, much like in the one before it, neither the array k nor its elements are literals. 在上面的行中,与前面的行非常相似,数组k及其元素都不是文字。 They are merely initialized with literals. 它们只是用文字初始化。 kp is a pointer that points to the first element of the array. kp是一个指向数组第一个元素的指针。 One can update the array with thos pointer; 可以使用thos指针更新数组; kp[1] = 3;

For int contestants[] = {1, 2, 3}; 对于int contestants[] = {1, 2, 3};

contestants is array of 3 items of type int initialized by 3 literals 1 , 2 and 3 . contestants是3项类型的阵列int由3个文本进行初始化的123

So literals are particular values written in the code and you should not mix terms literal (value of some type) and array (that also has a type, but is data structure). 因此,文字是在代码中编写的特定值,您不应混合术语文字(某种类型的值)和数组(也有类型,但是数据结构)。

Concerning your example with strings 关于你的字符串示例

char s[] = "How big is it?";  //array variable
char *t = s;  //pointer variable to a string literal

I understand t as a pointer to the first element of array, that was initialized with string literal. 我明白t作为指针数组的第一元素,将其用字符串文字初始化。

To start with, let's see some definitions. 首先,让我们看一些定义。

  • Array 排列

An array is an ordered data structure consisting of a collection of elements (values or variables), each identified by one (single dimensional array, or vector) or multiple indexes. 数组是一种有序数据结构,由一组元素(值或变量)组成,每个元素由一个(单维数组或向量)或多个索引标识。 The elemnts are stored in contiguous memory locations. 元素存储在连续的存储位置。

  • String Literals [From C11 standard, chapter 6.4.5 ] 字符串文字[来自C11标准,第6.4.5章]

A character string literal is a sequence of zero or more multibyte characters enclosed in double-quotes, as in "xyz" . 字符串文字是用双引号括起来的零个或多个多字节字符的序列,如"xyz"

  • Integer constants [From C11 standard, chapter 6.4.4.1 ] 整数常数[来自C11标准,第6.4.4.1章]

An integer constant begins with a digit, but has no period or exponent part. 整数常量以数字开头,但没有句点或指数部分。 It may have a prefix that specifies its base and a suffix that specifies its type. 它可能有一个指定其基数的前缀和一个指定其类型的后缀。

So, int x = 5; 所以, int x = 5; , here 5 is an integer constant. 这里5是整数常数。

  • Character constants [From C11 standard, chapter 6.4.4.4 ] 字符常数[来自C11标准,第6.4.4.4章]

An integer character constant is a sequence of one or more multibyte characters enclosed in single-quotes, as in 'x' . 整数字符常量是用单引号括起来的一个或多个多字节字符的序列,如'x'

So, char y = 'S'; 所以, char y = 'S'; , here 'S' is a character constant. ,这里'S'是一个字符常数。


Now, 现在,

  1. int contestants[] = {1, 2, 3};

contestants here is an integer array. 这里的contestants是一个整数数组。

  1. char s[] = "How big is it?";

s is a character array, being null terminated, can also be referred to as a string . s是一个字符数组,以null结尾,也可以称为字符串 OTOH, "How big is it?" OTOH, "How big is it?" is an unnamed string literal. 是一个未命名的字符串文字。 We're initializing the containts of s using the string literal. 我们正在使用字符串文字初始化 s的含义。 s in present in read-write memory and it's containts are modifiable. s目前在读写存储器中,它的含量是可修改的。

  1. char * point = "Hello World";

p is a pointer to the string literal "Hello World" . p是指向字符串文字"Hello World"的指针。 Usually it is stored in read-only memory location and alteration is not allowed. 通常它存储在只读存储器位置,不允许进行更改。

Strings: 字符串:

char s[] = "How big is it?"; //array variable

Here s is an array and holding the string and has both read and write option for this.You can modify the values of the array.The size of the string literal "How big is it?" 这里的s是一个数组并且持有字符串并且具有读取和写入选项。您可以修改数组的值。字符串文字的大小“它有多大?” is calculated and the array size is calculated based on the string length. 计算并根据字符串长度计算数组大小。

What is a string literal? 什么是字符串文字?

char *p = "someString";

Here the string someString is stored in a read-only location and the location address is returned to your pointer. 这里字符串someString存储在只读位置,位置地址返回到指针。 So now you can't write to the location the pointer is pointing to. 所以现在你不能写指针所指向的位置。

Integers: 整数:

int a[] = {1,2,3};

a is an array which holds values and they can be read also be modified. a是一个保存值的数组,它们也可以被修改。

In the code 在代码中

int i;
for(i=0;i<10;i++)

10 is a integer literal as we see 10 represents the decimal value and is directly included in the code. 10是整数文字,因为我们看到10表示十进制值并且直接包含在代码中。

One more example is 还有一个例子

int b;
b=1;

Now 1 is a integer literal. 现在1是整数字面值。

A literal is a syntactic form that directly represents a value in a programming language. 文字是一种直接表示编程语言中的值的语法形式。 Thus, 1 + 64 is an expression that evaluates to 65 and is not a literal; 因此, 1 + 64是一个表达式,其计算结果为65,而不是文字; x after int x = 65 also evaluates to 65 but is not a literal. xint x = 65还计算为65,但不是字面。 65 is a literal that represents 65, and 0x41 is the same; 65是表示65的文字, 0x41是相同的; 65L is also a literal that represents a "long integer" version of 65. 'A' is another literal that also represents the number 65 , this time as a char . 65L也是表示65的“长整数”版本的文字。 'A'是另一个也代表数字65文字,这次是作为char "ABC" is a string literal that, when put into code, represents a four-element array of characters, and fill it with values 65, 66, 67 and 0. You could also use the array literal (char[]){ 65, 66, 67, 0 } , and it would represent the same value, since strings are arrays of characters. "ABC"是一个字符串文字,当放入代码时,代表一个四元素的字符数组,并用值65,66,67和0填充它。 你也可以使用数组文字 (char[]){ 65, 66, 67, 0 } ,它代表相同的值,因为字符串是字符数组。 [See comments] [看评论]

Meanwhile, an array is a data structure that can contain multiple values, each value indexed by an integer. 同时,数组是一个可以包含多个值的数据结构,每个值都由一个整数索引。 Arrays can have literal syntax ( as demonstrated above eg in JavaScript), and literals can be of arrays; 数组可以有文字语法( 如上所示, 例如在JavaScript中),文字可以是数组; but the two are apples and oranges. 但这两个是苹果和橘子。

tl;dr: arrays are a specific kind of data structure; tl; dr:数组是一种特定的数据结构; literal is how you write data in code. literal是你在代码中编写数据的方式。

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

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