C 中的 "nonmodifiable" 与其他编程语言中的 "immutable" 是否相同?
Does "nonmodifiable" in C mean the same as "immutable" in other programming languages?
简而言之,从 C 语言来看,数组和字符串文字是不可修改的左值。
C 中的 "nonmodifiable" 是否与编程语言(例如 Python、Java 和函数式语言)中的 "immutable" 相同?
在编程语言设计中,数组的"nonmodifiable"和"immutable"对它的elements/items是否意味着相同?
为什么我们可以在 C 中修改数组和字符串文字中的元素的值,而我们不能在 Python 中修改字符串中的项目?
谢谢。
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
到此为止。你不能在 C 中修改字符串文字。你可以修改字符串的地址,但不能修改字符串本身。
这里有一个小程序来演示数组和字符串文字之间的区别:
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)
}
根据您对问题的措辞方式——指的是 "nonmodifiable lvalues"——我认为答案是:不,不可修改与不可变不同,至少在本书的上下文中是这样正在读书。 "Immutable" 是面向对象语言中对象的 属性。 (在 C 语言中,我认为 const
对象也被认为是不可变的。)"lvalue" 与编译器如何 class 化表达式有更多关系。按照我的理解,"lvalue" 是一个引用对象的表达式,而 "rvalue" 不是。您不能将右值放在赋值的左侧。大多数左值都可以赋值,但有些不能。根据语言规则,字符串文字被视为左值,但您不能说 "abc" = "xyz";
。这就是为什么它被认为是不可修改的左值。
http://ieng9.ucsd.edu/~cs30x/Non-modifiable%20Lvalues.htm 很好地解释了这一点。除其他外,作者说
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 实际上可以让你修改任何东西,如果你知道怎么做的话。如果无法修改 C 中的某些内容,那是因为硬件 and/or 操作系统不允许。如果您 运行 使用的嵌入式系统没有内置的方法来防止写入一定范围的内存,那么您可以修改任何内容,包括您自己的代码。这里的要点是 C 中没有任何内容可以使尝试修改字符串文字是非法的。
在 Java 和 Python 中,相比之下,字符串是对象,因此您可以对它们执行的唯一操作是为字符串 class 定义的操作。而那些 classes 根本不提供允许您修改字符串的方法。这就是不可变的意思。这是一个适用于对象的术语,而您对 "nonmodifiable" 的使用实际上仅适用于某些语法类别的表达式。所以你根本不能把两者等同起来。
简而言之,从 C 语言来看,数组和字符串文字是不可修改的左值。
C 中的 "nonmodifiable" 是否与编程语言(例如 Python、Java 和函数式语言)中的 "immutable" 相同?
在编程语言设计中,数组的"nonmodifiable"和"immutable"对它的elements/items是否意味着相同?
为什么我们可以在 C 中修改数组和字符串文字中的元素的值,而我们不能在 Python 中修改字符串中的项目?
谢谢。
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
到此为止。你不能在 C 中修改字符串文字。你可以修改字符串的地址,但不能修改字符串本身。
这里有一个小程序来演示数组和字符串文字之间的区别:
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)
}
根据您对问题的措辞方式——指的是 "nonmodifiable lvalues"——我认为答案是:不,不可修改与不可变不同,至少在本书的上下文中是这样正在读书。 "Immutable" 是面向对象语言中对象的 属性。 (在 C 语言中,我认为 const
对象也被认为是不可变的。)"lvalue" 与编译器如何 class 化表达式有更多关系。按照我的理解,"lvalue" 是一个引用对象的表达式,而 "rvalue" 不是。您不能将右值放在赋值的左侧。大多数左值都可以赋值,但有些不能。根据语言规则,字符串文字被视为左值,但您不能说 "abc" = "xyz";
。这就是为什么它被认为是不可修改的左值。
http://ieng9.ucsd.edu/~cs30x/Non-modifiable%20Lvalues.htm 很好地解释了这一点。除其他外,作者说
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 实际上可以让你修改任何东西,如果你知道怎么做的话。如果无法修改 C 中的某些内容,那是因为硬件 and/or 操作系统不允许。如果您 运行 使用的嵌入式系统没有内置的方法来防止写入一定范围的内存,那么您可以修改任何内容,包括您自己的代码。这里的要点是 C 中没有任何内容可以使尝试修改字符串文字是非法的。
在 Java 和 Python 中,相比之下,字符串是对象,因此您可以对它们执行的唯一操作是为字符串 class 定义的操作。而那些 classes 根本不提供允许您修改字符串的方法。这就是不可变的意思。这是一个适用于对象的术语,而您对 "nonmodifiable" 的使用实际上仅适用于某些语法类别的表达式。所以你根本不能把两者等同起来。