2

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 ?

Lee William
  • 125
  • 1
  • 11

5 Answers5

4

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;

n. m. could be an AI
  • 112,515
  • 14
  • 128
  • 243
  • Comments are not for extended discussion; this conversation has been [moved to chat](http://chat.stackoverflow.com/rooms/74351/discussion-on-answer-by-n-m-whats-the-difference-between-these-4-itemcharacte). – Taryn Apr 02 '15 at 13:54
2

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.

Gopi
  • 19,784
  • 4
  • 24
  • 36
1

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.

VolAnd
  • 6,367
  • 3
  • 25
  • 43
1

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.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • Technically we should say `s` is an array which contains a string. The string may be smaller than the array. – M.M Apr 02 '15 at 05:46
  • 1
    @MattMcNabb Sir, I would object to the word _contains_, rather I would say _initalized with the value of the string literal_. Also, in this very case, `s` will have the exact same number of elements as the initializer literal, as we've not supplied the index explicitly, right? – Sourav Ghosh Apr 02 '15 at 05:49
  • In this case yes, but in other cases it might not. A *string* is defined as a series of characters followed by a null character. – M.M Apr 02 '15 at 05:54
  • @MattMcNabb Absolutely. I mentioned the null-termination part in point 2. – Sourav Ghosh Apr 02 '15 at 05:55
  • My point is that an object is different to the value contained in it. For example `int x;` then `x` is an object and it may contain values `1`, `2`, etc., or no value. `char s[]` is an array (not a string) and it may contain values such as a string, or it may contain characters that do not form a string. – M.M Apr 02 '15 at 06:03
  • "So, `char y = 'S';`, here S is a character constant." correctly you had to say "here `'S'` is a character constant.", hadn't you? as just `S` can't be anything, can it? – dhein Apr 02 '15 at 06:10
  • @Zaibis Ahh, sure. Typo. – Sourav Ghosh Apr 02 '15 at 06:57
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; 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 e.g. 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.

Amadan
  • 191,408
  • 23
  • 240
  • 301
  • 1
    +1, but the "array literals" are called array initializers and cannot be used throughout the rest of the code as say, integer or string literals. – Blagovest Buyukliev Apr 02 '15 at 05:35
  • @BlagovestBuyukliev: Good point. Not the best example for C. `[1, 2, 3]` from JavaScript, Ruby and Python would be a true array literal. – Amadan Apr 02 '15 at 05:37
  • Thanks! I found if I write: char *cards = "ABC"; it can not be update. But if I write: char *cards = (char[]) { 65, 66, 67, 0 }; it can be updated. Could you explain it from the memory perspective of view? it should have something to do with the storing method in memory. – Lee William Apr 02 '15 at 05:39
  • It has to do with when and where it is allocated. In C, string literals are immutable; they are compiled into the code. It's a C rule, as you quote in your OP. – Amadan Apr 02 '15 at 06:53