6.6 C-style strings

原完整教程鏈接:6.6 C-style strings

1.
/* 
   A C-style string is simply an array of characters that uses a null 
   terminator. A null terminator is a special character (‘\0’, ascii code 0) 
   used to indicate the end of the string. More generically, A C-style 
   string is called a null-terminated string.

   When printing a C-style string, std::cout prints characters until it 
   encounters the null terminator. If you accidentally overwrite the 
   null terminator in a string (e.g. by assigning something to 
   mystring[6]), you’ll not only get all the characters in the string, but 
   std::cout will just keep printing everything in adjacent memory 
   slots until it happens to hit a 0!
*/

/* 
   Although “string” only has 6 letters, C++ automatically adds a null 
   terminator to the end of the string for us (we don’t need to include 
   it ourselves). Consequently, mystring is actually an array of length 7!
*/
char mystring[] = "string";

2.
// The recommended way of reading strings using cin is as follows:
/*
   This call to cin.getline() will read up to 254 characters into name 
   (leaving room for the null terminator!). Any excess characters will 
   be discarded. In this way, we guarantee that we will not overflow the array!
*/
#include <iostream>
int main()
{
    char name[255]; // declare array large enough to hold 255 characters
    std::cout << "Enter your name: ";
    std::cin.getline(name, 255);
    std::cout << "You entered: " << name << '\n';
 
    return 0;
}

3.
// Don’t use C-style strings, use std::string (defined in the <string> 
// header) instead. 

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市鼠冕,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖冻记,帶你破解...
    沈念sama閱讀 211,194評論 6 490
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件蔽莱,死亡現(xiàn)場離奇詭異默赂,居然都是意外死亡,警方通過查閱死者的電腦和手機制轰,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,058評論 2 385
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來胞谭,“玉大人垃杖,你說我怎么就攤上這事≌梢伲” “怎么了调俘?”我有些...
    開封第一講書人閱讀 156,780評論 0 346
  • 文/不壞的土叔 我叫張陵,是天一觀的道長旺垒。 經(jīng)常有香客問我彩库,道長,這世上最難降的妖魔是什么先蒋? 我笑而不...
    開封第一講書人閱讀 56,388評論 1 283
  • 正文 為了忘掉前任骇钦,我火速辦了婚禮,結(jié)果婚禮上竞漾,老公的妹妹穿的比我還像新娘司忱。我一直安慰自己,他們只是感情好畴蹭,可當(dāng)我...
    茶點故事閱讀 65,430評論 5 384
  • 文/花漫 我一把揭開白布坦仍。 她就那樣靜靜地躺著,像睡著了一般叨襟。 火紅的嫁衣襯著肌膚如雪繁扎。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 49,764評論 1 290
  • 那天,我揣著相機與錄音梳玫,去河邊找鬼爹梁。 笑死,一個胖子當(dāng)著我的面吹牛提澎,可吹牛的內(nèi)容都是我干的姚垃。 我是一名探鬼主播,決...
    沈念sama閱讀 38,907評論 3 406
  • 文/蒼蘭香墨 我猛地睜開眼积糯,長吁一口氣:“原來是場噩夢啊……” “哼谦纱!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起川慌,我...
    開封第一講書人閱讀 37,679評論 0 266
  • 序言:老撾萬榮一對情侶失蹤祠乃,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后亮瓷,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 44,122評論 1 303
  • 正文 獨居荒郊野嶺守林人離奇死亡艾蓝,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 36,459評論 2 325
  • 正文 我和宋清朗相戀三年赢织,在試婚紗的時候發(fā)現(xiàn)自己被綠了馍盟。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 38,605評論 1 340
  • 序言:一個原本活蹦亂跳的男人離奇死亡八毯,死狀恐怖瞄桨,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情芯侥,我是刑警寧澤乳讥,帶...
    沈念sama閱讀 34,270評論 4 329
  • 正文 年R本政府宣布廓俭,位于F島的核電站,受9級特大地震影響汹忠,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜宽菜,卻給世界環(huán)境...
    茶點故事閱讀 39,867評論 3 312
  • 文/蒙蒙 一橄唬、第九天 我趴在偏房一處隱蔽的房頂上張望仰楚。 院中可真熱鬧,春花似錦僧界、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,734評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至纽帖,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間扒吁,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 31,961評論 1 265
  • 我被黑心中介騙來泰國打工雕崩, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留融撞,地道東北人。 一個月前我還...
    沈念sama閱讀 46,297評論 2 360
  • 正文 我出身青樓捉貌,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子牧挣,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 43,472評論 2 348

推薦閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 171,747評論 25 707
  • WebSocket-Swift Starscream的使用 WebSocket 是 HTML5 一種新的協(xié)議。它實...
    香橙柚子閱讀 23,794評論 8 183
  • 樂意小朋友今天學(xué)了個滑板上的新動作刨摩,驕傲的不得了,沖著正在做飯的我大喊:“媽媽呻征,快看!我學(xué)會個新動作罢浇!” 再說說...
    我超樂意閱讀 242評論 0 0
  • 還在留戀全馬時刻! 9月23 領(lǐng)沈陽馬拉松 物品包的最后一天攒岛。我們一家三口來到了 渾南奧體 人還行 不是很擁擠胞锰。可...
    3109閱讀 552評論 2 1