Home > Blockchain >  What does the following mean apropos C programming language?
What does the following mean apropos C programming language?

Time:11-28

A book on C programming says,

"There is another family of read-only objects that unfortunately are not protected by their type from being modified: string literals.

Takeaway - String literals are read-only.

If introduced today, the type of string literals would certainly be char const[], an array of const-qualified characters. Unfortunately, the const keyword was introduced to the C language much later than string literals, and therefore it remained as it is for backward compatibility."

Question 1. How can strings be read only, like takeaway says, if they can be modified?

Question 2. "There is another family of read-only objects that unfortunately are not protected by their type from being modified: string literals."

What type is this referring to, which doesn't keep a string literal from being modified?

Question 3. If string literals were introduced today and had the type char const[], how'll they be an array i.e. I can't grasp as to how string literals will be an array of const qualified characters?

CodePudding user response:

How can strings be read only, like takeaway says, if they can be modified?

Because Unfortunately, the const keyword was introduced to the C language much later than string literals, and therefore it remained as it is for backward compatibility.

String literals existed before protection (in the form of const keyword) existed in the language.

Ergo, they are not protected, because the tools to protect it did not exist. A classic example of undefined behavior is writing to a string literal, see Undefined, unspecified and implementation-defined behavior .

What type is this referring to, which doesn't keep a string literal from being modified?

It has the type char[N] - "array of N chars" - where N is the number of characters in the string plus one for zero terminating character.

See https://en.cppreference.com/w/c/language/string_literal .

char *str = "string literal";
      // ^^^ - implicit decay from `char [N]` -> `char *`
str[0] = 'a'; // compiler will be fine, but it's invalid code

// or super shorter form:
"string literal"[0] = 'a'; // invalid code

If string literals were introduced today and had the type char const[], how'll they be an array i.e. I can't grasp as to how string literals will be an array of const qualified characters?

The type would be const char[N] - an array of N constant chars, which means you can't modify the characters.

// assuming string literal has type const char [N]

const char *str = "string literal";
            // ^^^ - implicit decay from `const char [N]` -> `const char *`
str[0] = 'a'; // compile time error

// or super shorter form:
"string literal"[0] = 'a'; // with `const char [N]` would be a compiler time error

With gcc compiler use -Wwrite-strings to protect against mistakes like writing to string literals.

CodePudding user response:

Question 1. How can strings be read only, like takeaway says, if they can be modified?

The text does not say they can be modified. It says they are not protected from being modified. That is a slight error; properly, it should say they are not protected from attempts to modify them: The rules of the C standard do not prevent you from writing code that attempts to modify a string literal, and they do not define the results when a program executes such an attempt. In some circumstances, attempting to modify a string literal may result in a signal, usually ending program execution by default. In other circumstances, the attempt may succeed, and the string literal will be modified. In other circumstances, nothing will happen; there will be neither a signal nor a change to the string literal. It is also possible other behaviors may occur.

Question 2. "There is another family of read-only objects that unfortunately are not protected by their type from being modified: string literals."

What type is this referring to, which doesn't keep a string literal from being modified?

Technically, a string literal is a piece of source code that has a character sequence inside quotes, optionally with an encoding prefix. During compilation or program execution, an array is generated with the contents of the character sequence and a terminating null character. For string literals without a prefix, the type of that array is char []. (If there is a prefix, the type may also be wchar_t [], char16_t [], or char32_t, depending on the prefix.)

Colloquially, we often refer to this array as the string literal, even though the array is the thing that results from a string literal (an array in memory) not the actual string literal (in the source code).

The type char [] does not contain const, so it does not offer the protections that const char [] does. (Those protections are fairly mild.)

Question 3. If string literals were introduced today and had the type char const[], how'll they be an array i.e. I can't grasp as to how string literals will be an array of const qualified characters?

Your confusion here is unclear. When a string literal appears in source code, the compiler arranges for its contents to be in the memory of the running program. Those contents are in memory as an array of characters. If the rules of C were different, the type of that array would be const char [] instead of char [].

  • Related