關於C 中c str 的問題

時間 2021-11-04 03:30:46

1樓:匿名使用者

the function c_str() returns a const pointer to a regular c string, identical to the current string. the returned string is null-terminated.

c_str函式的返回值是const char*的,不能直接賦值給char* ,所以就需要我們進行相應的操作轉化(利用strcpy()函式),或用const char *ch試試

2樓:匿名使用者

#include

#include

using namespace std;

int main(void)

3樓:

應該是:

const char * ch = str.c_str();

4樓:

怎麼報錯,報什麼錯?說得詳細點呀,it工程技術網上有很多c++基礎知識,你可以去看看。

c++中,c_str()的用法

5樓:手機使用者

返回c風格的字串。

如:#include

const string str="abc";

const char *c=str.c_str();

cout<

6樓:椎冰

就是把 string 轉成 char*

string str="yoooo";

char *a=str.c_str();

7樓:匿名使用者

這個函式是 string 返回 char* 用的。例如:

#include

#include

using namespace std;

void main()

8樓:小小我笑笑哈

包含在標頭檔案cstring中

c++中c_str()函式是什麼作用? - c / c++ -

9樓:匿名使用者

c_str函式的返回值是const char*的,不能直接賦值給char*,所以就需要我們進行相應的操作轉化,下面就是這一轉化過程。 c++語言提供了兩種字串實現,其中較原始的一種只是字串的c語言實現。與c語言的其他部分一樣,它在c+的所有實現中可用,我們將這種實現提供的字串物件,歸為c-串,每個c-串char*型別的。

標準標頭檔案包含操作c-串的函式庫。這些庫函式表達了我們希望使用的幾乎每種字串操作。 當呼叫庫函式,客戶程式提供的是string型別引數,而庫函式內部實現用的是c-串,因此需要將string物件,轉化為char*物件,而 c_str()提供了這樣一種方法,它返回一個客戶程式可讀不可改的指向字元陣列的指標。

例: #include [i] #include using std::string;void main() { string add_to=\"hello!

\"; //std::cout和有什麼不同?為什麼有時候看見是,有時候又是呢?

還有就是為什麼要在前面加上const呀,有什麼用嗎,麻煩了。

10樓:匿名使用者

再弱弱地問一下,.c_str()是標準庫裡面的函式嗎?還有就是為什麼要在前面加上const呀,有什麼用?

c++中的一個語句,atoi(s.substr(5,2).c_str());我不明白c_str()的作用是什麼?

11樓:匿名使用者

下面是atoi標頭檔案中的原型。

int atoi( const char *str );

c++是強型別的語言

實參與函式宣告中的引數型別必須一致

12樓:匿名使用者

char * 型和string 是完全不同的東西

char *型字串是一個簡單的字元陣列,一般會以'\0'結尾,沒有其它多餘東西

string不一樣,它是一個封裝的類,是stl中提供的一個支援很多高階操作的資料型別,建議你參考一下c++教程介紹string的部分吧

關於c 中const問題

include using namespace std void printvalues const int beg,const int end while beg end cout beg printvalues j,j 2 return 0 自己多多測試就會明白的。希望能看懂o o const ...

C中關於引數按引用傳遞的問題,C 中的out引數呼叫方法的時候需要傳遞對應的值嗎?

你的 這個方法 swap textbox t1,textbox t2 是交換好 變數後 又重新給文字框進行了賦值操作,當然文字框的值我們看到了改變!再看你的這個方法 swap textbox t1,textbox t2 同時2個變數,通過除錯,發現 textbox t3 new textbox t3...

c 中關於私有靜態變數的問題

include a.h void main a.h如下 class a a static void method private static int a static int b static int c 檔案a.cpp void a method 沒見過有寫 include a.cpp 應該寫成...