简体   繁体   中英

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.

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"

Character literal:'a'

Integer literal:1

I'm messed by these 4 item: character,array,string,literal . 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 .

int contestants[] = {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.

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.

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.

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

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

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;

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;

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;

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

contestants is array of 3 items of type int initialized by 3 literals 1 , 2 and 3 .

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.

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 ]

A character string literal is a sequence of zero or more multibyte characters enclosed in double-quotes, as in "xyz" .

  • Integer constants [From C11 standard, chapter 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; , here 5 is an integer constant.

  • Character constants [From C11 standard, chapter 6.4.4.4 ]

An integer character constant is a sequence of one or more multibyte characters enclosed in single-quotes, as in 'x' .

So, char y = 'S'; , here 'S' is a character constant.


Now,

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

contestants here is an integer array.

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

s is a character array, being null terminated, can also be referred to as a string . OTOH, "How big is it?" is an unnamed string literal. We're initializing the containts of s using the string literal. s in present in read-write memory and it's containts are modifiable.

  1. char * point = "Hello World";

p is a pointer to the string literal "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?" 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. 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.

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.

One more example is

int b;
b=1;

Now 1 is a integer literal.

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; x after int x = 65 also evaluates to 65 but is not a literal. 65 is a literal that represents 65, and 0x41 is the same; 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 . "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. [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; but the two are apples and oranges.

tl;dr: arrays are a specific kind of data structure; literal is how you write data in code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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