Tim
Tim

Reputation: 99488

Does "nonmodifiable" in C mean the same as "immutable" in other programming languages?

From C in a Nutshell, arrays and string literals are nonmodifiable lvalues.

Does "nonmodifiable" in C mean the same as "immutable" in programming languages (e.g. Python, Java, and functional languages)?

In programming language design, does "nonmodifiable" and "immutable" of an array imply the same for its elements/items?

Why can we modify the value of an element of an array and string literals in C, while we can't modify an item in a string in Python?

Thanks.

Upvotes: 0

Views: 127

Answers (2)

ajb
ajb

Reputation: 31699

Based on the way you worded the question--referring to "nonmodifiable lvalues"--I think the answer is: no, nonmodifiable isn't the same as immutable, at least in the context of the book you're reading. "Immutable" is a property of objects in object-oriented languages. (In C, I think a const object would also be considered immutable.) "lvalue" has more to do with how the compiler classifies an expression. The way I understand it, an "lvalue" is an expression that refers to an object, and an "rvalue" isn't. You can't put an rvalue on the left side of an assignment. Most lvalues can be assigned to, but some can't. A string literal is considered an lvalue, according to the language rules, but you can't say "abc" = "xyz";. That's why it's considered a nonmodifiable lvalue.

http://ieng9.ucsd.edu/~cs30x/Non-modifiable%20Lvalues.htm does a good job of explaining this. Among other things, the author says

Notice that I did not say a non-modifiable lvalue refers to an object that you can't modify-I said you can't use the lvalue to modify the object.

C will actually let you modify anything, if you know how to do it. If something in C can't be modified, it's because the hardware and/or operating system won't let you. If you're running on a embedded system that doesn't have a built-in way of preventing writes to a range of memory, then you can modify anything including your own code. The point here is that nothing in C makes it illegal to try to modify a string literal.

In Java and Python, by contrast, strings are objects, and as such the only operations you can perform on them are the ones defined for the string class. And those classes simply don't provide methods that allow you to modify the strings. That's what is meant by immutable. It's a term that applies to objects, while your use of "nonmodifiable" really applies only to certain syntactical categories of expressions. So you can't really equate the two at all.

Upvotes: 1

savram
savram

Reputation: 580

Does "nonmodifiable" in C mean the same as "immutable" in programming languages (e.g. Python, Java, and functional languages)?

Yes.

In programming language design, does "nonmodifiable" and "immutable" of an array imply the same for its elements/items?

No. Arrays are immutable in the sense that you can not modify the address it stores, which points to the first element of the sequence. You can, however, modify the elements of the array.

Why can we modify the value of an element of an array and string literals in C

Stop right there. You can't modify string literals in C. You can modify the address of the string, but not the string itself.

Here's a little program to demonstrate the difference between arrays and string literals:

int main(){
  char* a = "somestring";
  char* b = "oldstring";
  char c[] = "somestring2";
  b = a;//OK, you modified the address. Now it stores the address of the first char from string a
  *b = 'l';//not OK, you modified the string(its first character)
  c = a;//not ok, you're changing the address the array holds
  *c = 'j';//OK, modifying array's elements(first char)
}

Upvotes: 1

Related Questions