久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

修改 char *const 字符串

Modifying a char *const string(修改 char *const 字符串)
本文介紹了修改 char *const 字符串的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問(wèn)題描述

限時(shí)送ChatGPT賬號(hào)..

我知道const char * 是一個(gè)指向const char 的指針,而char *const 是一個(gè)指向char 的常量指針.我正在以下代碼中對(duì)此進(jìn)行測(cè)試:

I know that const char * is a pointer to a const char, while char *const is a constant pointer to a char. I am testing this in the following code:

const char *s = "hello";    // Not permitted to modify the string "hello"
char *const t = "world";    // Not permitted to modify the pointer t

s = "hello2";   // Valid
// t = "world2";   // Invalid, gives compilation error

// *(s + 1) = 'a';    // Invalid, gives compilation error
*(t + 1) = 'a';       // Why does this not work?    

最后一行沒(méi)有給出任何錯(cuò)誤,而是導(dǎo)致程序意外終止.為什么不允許修改t指向的字符串?

The last line does not give any error, but causes the program to terminate unexpectedly. Why is modifying the string pointed to by t not allowed?

推薦答案

t 指向一個(gè)字符串文字 修改字符串文字是未定義的行為.C++ 草案標(biāo)準(zhǔn)部分 2.14.5 String literals 段落 12 說(shuō)(強(qiáng)調(diào)我的):

t is pointing to a string literal it is undefined behavior to modify a string literal. The C++ draft standard section 2.14.5 String literals paragraph 12 says(emphasis mine):

是否所有字符串文字都是不同的(即存儲(chǔ)在非重疊對(duì)象中)是實(shí)現(xiàn)定義的.嘗試修改字符串文字的效果未定義.

Whether all string literals are distinct (that is, are stored in nonoverlapping objects) is implementation defined. The effect of attempting to modify a string literal is undefined.

C99 草案標(biāo)準(zhǔn)的相關(guān)部分是 6.4.5 String literals 段落 6 說(shuō)(強(qiáng)調(diào)我的em>):

The relevant section from the C99 draft standard is 6.4.5 String literals paragraph 6 which says(emphasis mine):

未指定這些數(shù)組是否不同,前提是它們的元素具有適當(dāng)?shù)闹?如果程序試圖修改這樣的數(shù)組,行為是未定義.

It is unspecified whether these arrays are distinct provided their elements have the appropriate values. If the program attempts to modify such an array, the behavior is undefined.

在典型的現(xiàn)代 Unix 平臺(tái)上,您會(huì)在只讀段中找到字符串文字,如果我們嘗試修改它,這將導(dǎo)致訪問(wèn)沖突.我們可以使用 objdump 來(lái)檢查只讀部分,如下所示:

On a typical modern Unix platform you will find string literals in the read-only segment which would result in a access violation if we attempt to modify it. We can use objdump to inspect the read-only section as follows:

objdump -s -j .rodata

我們可以在下面的實(shí)際示例中看到確實(shí)會(huì)找到字符串文字在只讀部分.請(qǐng)注意,我必須添加一個(gè) printf 否則編譯器會(huì)優(yōu)化出字符串文字.示例 `objdump 輸出:

we can see in the following live example that the string literal will indeed be found in the read-only section. Note that I had to add a printf otherwise the compiler would optimize out the string literal. Sample `objdump output:

Contents of section .rodata:
 400668 01000200 776f726c 64002573 0a00      ....world.%s..

另一種方法是讓 t 指向一個(gè)帶有 字符串文字 副本的數(shù)組,如下所示:

An alternative approach would be to have t point to an array with a copy of a string literal like so:

char r[] = "world";    
char *const t = r ;

這篇關(guān)于修改 char *const 字符串的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Difference between std::reference_wrapper and simple pointer?(std::reference_wrapper 和簡(jiǎn)單指針的區(qū)別?)
Difference between const. pointer and reference?(常量之間的區(qū)別.指針和引用?)
How to access the contents of a vector from a pointer to the vector in C++?(c++ - 如何從指向向量的指針訪問(wèn)向量的內(nèi)容?)
Meaning of *amp; and **amp; in C++(*amp; 的含義和**amp;在 C++ 中)
Why can#39;t I do polymorphism with normal variables?(為什么我不能對(duì)普通變量進(jìn)行多態(tài)?)
Dereferencing deleted pointers always result in an Access Violation?(取消引用已刪除的指針總是會(huì)導(dǎo)致訪問(wèn)沖突?)
主站蜘蛛池模板: 国产美女一区二区 | 国产精品99久久久久久www | 中文字幕日韩一区二区 | 国产精品久久久久久久久久久久午夜片 | 精区3d动漫一品二品精区 | 日韩精品一区二区三区四区 | 在线观看av不卡 | 中文字幕不卡在线观看 | 一区二区激情 | 久久精品屋 | 久久精品中文字幕 | 国产999精品久久久久久 | 在线亚州 | 天天躁日日躁xxxxaaaa | 国产欧美精品区一区二区三区 | 久久国产精品偷 | 久久久久久久久综合 | 99久久电影 | 成人免费视频网站 | 成人午夜网站 | 福利视频大全 | 亚洲精品乱码久久久久久9色 | 精品国产乱码一区二区三区a | 一区二区三区国产精品 | av影音资源| 久久日韩粉嫩一区二区三区 | 国产欧美一级 | 精品中文字幕在线 | 成人免费视频网站在线观看 | 欧美日韩电影一区二区 | 最新国产精品视频 | 亚洲国产精品一区二区三区 | 欧美激情在线精品一区二区三区 | 99精品视频一区二区三区 | 免费在线一区二区三区 | 免费特级黄毛片 | 黄色网毛片 | 亚洲精品专区 | 久久综合久色欧美综合狠狠 | 日韩欧美久久精品 | 久久网亚洲 |