第一种方法中的字符串''hello''如何位于只读内存中?和第二种方法中的字符串''hello''如何位于可修改的内存中? 只有''const''在C中提供''只读''功能。怎么来 这个char * s提供该功能?实际上这个功能的内部是什么? 以下是提示我的信息的快照 提出这个问题: - 在任何情况下,char * s =" Hello" ;;只是意味着指针s被分配了字符串文字Hello的地址。。通常情况下, 字符串文字将驻留在只读内存中,这意味着它不是合法的: char * s =你好; s [1] =''a''; 虽然它是完全合法的 char s [] =" Hello" ;; s [1] =''a''; 优先购买者, Karthik BalaguruHi,While trying to understand the difference between the following 2methods, i have some interesting queries.Method 1) char *s = "Hello";andMethod 2) char s[] = "Hello";How does the string ''hello'' in first method lie in read-only memoryand the string ''hello'' in second method lie in a modifiable memory ?Only ''const'' provides the ''Read-only'' functionality in C . How comethis "char *s" provides that functionality ? What is the internal ofthis functionality actually ?The following is the snapshot of the info that has prompted me toraise this query :-In any context, char *s = "Hello"; just means that the pointer s isassigned the address of the string literal "Hello". Normally, thatstring literal will reside in read-only memory which means that it''snot legal to do:char *s = "Hello"; s[1] = ''a'';while it''s perfectly legal to dochar s[] = "Hello"; s[1] = ''a'';Thx in advans,Karthik Balaguru推荐答案 karthikbalaguru< ka *************** @ gmail.comwrote:karthikbalaguru <ka***************@gmail.comwrote: 在尝试理解以下2 方法之间的区别时,我有一些有趣的查询。 方法1)char * s =" Hello"; 和 方法2)char s [] =" Hello" ;; 字符串''hello''如何第一种方法在于只读存储器While trying to understand the difference between the following 2methods, i have some interesting queries.Method 1) char *s = "Hello";andMethod 2) char s[] = "Hello";How does the string ''hello'' in first method lie in read-only memory 谁说它呢?它_may_,因为它是一个字符串文字,而字符串 文字是允许不可修改的。确切地说,修改为保存字符串文字而创建的数组的 内容具有 未定义的行为,这意味着任何事情都可能发生,来自 修改成功,通过它被忽略,崩溃程序, 如果实施是故意恶意的,那么任何东西都会发送给你的电子邮件命名为CERT-In。Who says it does? It _may_, because it''s a string literal, and stringliterals are allowed to be non-modifiable. To be exact, modifying thecontents of the array that is created to hold the string literal hasundefined behaviour, which means that anything may happen, from themodification succeeding, through it being ignored, crashing the program,and if the implementation is being intentionally malevolent, anything upto sending lurid emails in your name to CERT-In. 和第二种方法中的字符串''hello'位于可修改的内存中?and the string ''hello'' in second method lie in a modifiable memory ? 因为它是一个普通的对象,它是从字符串初始化的 literal;但是一旦初始化,它就是一个正常的,可修改的数组对象。Because it''s a normal object, which is initialised from a stringliteral; but once initialised, it is a normal, modifiable array object. 只有''const''在C中提供''只读''功能。Only ''const'' provides the ''Read-only'' functionality in C . 错误。Wrong. 为什么这个char * s提供该功能?How come this "char *s" provides that functionality ? 它不是;它指向一个字符串文字的事实。如果你把指针指向一个char数组_object_而不是一个字符串 literal,你可以修改它。It doesn''t; the fact that it points at a string literal does. Had youpointed the pointer at a char array _object_ rather than at a stringliteral, you could have modified it. 实际上这个功能的内部是什么?What is the internal of this functionality actually ? 这句话毫无意义。 RichardThat sentence no meaning.Richard karthikbalaguru写道:karthikbalaguru wrote: 在尝试理解以下2 方法之间的区别时,我有一些有趣的疑问。 方法1)char * s =" Hello"; 和 方法2)char s [] =" Hello"; 第一种方法中字符串''hello''如何位于只读存储器中While trying to understand the difference between the following 2methods, i have some interesting queries.Method 1) char *s = "Hello";andMethod 2) char s[] = "Hello";How does the string ''hello'' in first method lie in read-only memory 它/可能/位于只读存储器中。这不是/要求/。It /may/ lie in read-only memory. It''s not /required/ to. 和第二种方法中的字符串''hello''位于可修改的内存中?and the string ''hello'' in second method lie in a modifiable memory ? 标准说它写入字符串是未定义的行为 literal。因此,一个实现可以将字符串文字放在只读的内存中,因为如果有人写入它们,标准会说所有 Bets Are Off。The standard says it''s undefined behaviour to write into a stringliteral. So an implementation can put string literals in read-onlymemory because, if anyone writes into them, the standard says AllBets Are Off. 只有''const''在C中提供''只读''功能。Only ''const'' provides the ''Read-only'' functionality in C . 不是真的。Not really true. 为什么这个char * s提供该功能?How come this "char *s" provides that functionality ? 它没有。这与`char *`ness无关。它与 你好有关。是一个字符串文字。你不允许修改 的内容。 在第二次初始化时,`char s [] =" Hello";`, / content / 该文字的被复制到为`s`分配的商店中。 (并且 内容没有变化,因为如果你尝试过,你会得到UB。) 因为你无法访问内部字面意思,无论是否b 恰好只读是无关紧要的。实际上, 的实现可能根本不需要保留文字。 [它可能作为特定于实现的示例实现 char s [] =" f"; 函数内部 mov r0, #''f'' str r0,[fp],#offsetOf(s) 这样文字字符串的内容最终编码为 `ldr`指令的直接值。] - 克里斯绝杀的长臂 ; Dollin Hewlett-Packard Limited注册办公室:Cain Road,Bracknell, 注册号:690597 England Berks RG12 1HNIt doesn''t. It''s not to do with `char *` ness. It''s to do with"Hello" being a string literal. You''re not allowed to modifythe contents thereof.In the second initialisation `char s[] = "Hello";`, the /content/of that literal is copied into the store allocated for `s`. (Andthat content doesn''t change, since if you tried, you''d get UB.)Since you don''t get access to the insides of the literal, whetheror not it happened to be read-only doesn''t matter. Indeed, theimplementation might not need to keep the literal around at all.[It might, as an implementation-specific example, implementchar s[] = "f";inside a function asmov r0, #''f''str r0, [fp], #offsetOf(s)so that the content of the literal string ends up encoded inthe immediate value of the `ldr` instruction.]--Chris "the long arm of the lore" DollinHewlett-Packard Limited registered office: Cain Road, Bracknell,registered no: 690597 England Berks RG12 1HN karthikbalaguru写道:karthikbalaguru wrote: > 在尝试理解以下2 方法之间的区别时,我有一些有趣的查询。 方法1)char * s =" Hello"; 和 方法2)char s [] =" Hello" ;; 字符串''你好'怎么样?第一种方法是在只读内存中>Hi,While trying to understand the difference between the following 2methods, i have some interesting queries.Method 1) char *s = "Hello";andMethod 2) char s[] = "Hello";How does the string ''hello'' in first method lie in read-only memory 以下代码应该做什么? char * s ="你好; * s =''\ 0''; put(你好); 即使该代码的含义没有明确说明,因为C标准未定义, i t显然是含糊不清的代码。 - peteWhat should the following code do?char *s = "Hello";*s = ''\0'';puts("Hello");Even if the meaning of that code wasn''t explicitlymade undefined by the C standard,it is obviously ambiguous code.--pete 这篇关于没有'const'的只读功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!