第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

為什么將char指針流傳輸?shù)絚out不能打印地址?

為什么將char指針流傳輸?shù)絚out不能打印地址?

C++
紫衣仙女 2019-12-06 15:47:08
當(dāng)我使用來打印char指針時(shí)printf(),它使用轉(zhuǎn)換說明符來決定是否應(yīng)打印地址或根據(jù)%u或%s來打印整個(gè)字符串。但是當(dāng)我想使用相同的方法時(shí)cout,如何cout決定在地址和整個(gè)字符串之間應(yīng)該打印什么呢?這是一個(gè)示例源:int main(){  char ch='a';  char *cptr=&ch;  cout<<cptr<<endl;  return 0;}在這里,在我的GNU編譯器中,cout正在嘗試將ch輸出為字符串。我如何獲得ch通過cptr使用的地址cout?
查看完整描述

3 回答

?
MMTTMM

TA貢獻(xiàn)1869條經(jīng)驗(yàn) 獲得超4個(gè)贊

重載分辨率選擇ostream& operator<<(ostream& o, const char *c);用于打印C樣式字符串的。您要ostream& operator<<(ostream& o, const void *p);選擇另一個(gè)。您可能最好在這里進(jìn)行演員表:


 cout << static_cast<void *>(cptr) << endl;


查看完整回答
反對(duì) 回復(fù) 2019-12-06
?
慕工程0101907

TA貢獻(xiàn)1887條經(jīng)驗(yàn) 獲得超5個(gè)贊

cout如果收到一個(gè)字符串,則打印一個(gè)字符串,就這么char *簡單。


下面是重載operator <<的ostream:


ostream& operator<< (bool val);

ostream& operator<< (short val);

ostream& operator<< (unsigned short val);

ostream& operator<< (int val);

ostream& operator<< (unsigned int val);

ostream& operator<< (long val);

ostream& operator<< (unsigned long val);

ostream& operator<< (float val);

ostream& operator<< (double val);

ostream& operator<< (long double val);

ostream& operator<< (const void* val);


ostream& operator<< (streambuf* sb);


ostream& operator<< (ostream& ( *pf )(ostream&));

ostream& operator<< (ios& ( *pf )(ios&));

ostream& operator<< (ios_base& ( *pf )(ios_base&));


ostream& operator<< (ostream& out, char c );

ostream& operator<< (ostream& out, signed char c );

ostream& operator<< (ostream& out, unsigned char c );



//this is called

ostream& operator<< (ostream& out, const char* s );

ostream& operator<< (ostream& out, const signed char* s );

ostream& operator<< (ostream& out, const unsigned char* s );

如果需要地址,則需要:


ostream& operator<< (const void* val);

因此您需要轉(zhuǎn)換為const void*。


查看完整回答
反對(duì) 回復(fù) 2019-12-06
?
小唯快跑啊

TA貢獻(xiàn)1863條經(jīng)驗(yàn) 獲得超2個(gè)贊

我只是將其轉(zhuǎn)換為void *,因此它不會(huì)嘗試將其解釋為C字符串:


cout << (void*) cptr << endl;

但是,一個(gè)更安全的選擇是使用dirkgently的答案中的static_cast(這樣,至少在編譯時(shí)檢查了強(qiáng)制類型轉(zhuǎn)換)。


查看完整回答
反對(duì) 回復(fù) 2019-12-06
  • 3 回答
  • 0 關(guān)注
  • 640 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)