本文介绍了这4个项目之间有什么区别:字符,数组,字符串,文字.在C中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近阅读C书时,有些基本概念使我感到困惑.

there's some basic concepts that I get confused when reading a C book recently.

它说:指向字符串文字的变量不能用于更改字符串的内容.

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?"

字符串文字:"ABC"

string literal:"ABC"

字符文字:'a'

整数文字:1

我对这4个项目感到困惑:字符,数组,字符串,文字.我还是很困惑.

I'm messed by these 4 item:character,array,string,literal. I'm still very messed up.

字符数组字符串文字是同一回事吗?

字符数组数组文字是否相同?

推荐答案

文字是程序文本中表示值的令牌.有字符串文字,例如"123" ,字符文字,例如'a',数字文字,例如 7 .

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.

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

在上面的程序片段中,有三个文字 1 2 3 ,没有其他文字.特别是,选手 {1、2、3} 都不是文字.

In the program fragment above there are three literals 1 2 and 3 and no others. In particular, neither contestants nor {1, 2, 3} are literals.

值得注意的是,C标准仅在引用字符串文字时使用文字文字.另一种正式称为常量.但是您可能在所有地方都将它们称为文字,因此我在此处将它们包括在内.整数文字"和整数常量"是同一回事.

It is worth noting that the C standard uses the word literal only in reference to string literals. 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.

字符串文字也是程序中的一个对象(一条数据,一个存储区),与以前意义上的字符串文字相关联.这段数据是一个字符数组.并非每个字符数组都是字符串文字.没有 int 数组是文字.

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.

指针可以指向字符串文字,但不能指向字符文字或整数文字,因为后两种不是对象(没有与之关联的存储).指针只能指向一个对象.您不能将下注者指向文字5.因此,不会出现是否可以修改此类问题的问题.

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.

char* p = "123";

在上面的程序片段中,"123" 是文字, p 指向它.您不能修改 p 指向的对象.

In a program fragment above, "123" is a literal and p points to it. You cannot modify an object pointed to by p.

char a[] = "123";

在上面的程序片段中, a 是一个字符数组.它用字符串文字"123"初始化,但它本身不是文字,可以自由修改.

In the program fragment above, a is a character array. It is initialized with a string literal "123", but it is not a literal itself and can be modified freely.

int i = 5;

以上, 5 是文字,而 i 不是. i 是用文字初始化的,但它本身不是一个.

Above, 5 is a literal and i is not. i is initialized with a literal, but it isn't one itself.

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

在上面的行中,就像在前面的行中一样,数组 k 或其元素都不是文字.它们只是用文字初始化. kp 是一个指向数组第一个元素的指针.可以使用thos指针更新数组. kp [1] = 3;

In the line above, much like in the one before it, neither the array k nor its elements are literals. They are merely initialized with literals. kp is a pointer that points to the first element of the array. One can update the array with thos pointer; kp[1] = 3;

这篇关于这4个项目之间有什么区别:字符,数组,字符串,文字.在C中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 23:34