<span style="font-family: Arial, Helvetica, sans-serif;"><span style="white-space:pre"> </span> char a[6] = "12345";</span><span style="font-family: Arial, Helvetica, sans-serif;"> </span>//該內存在棧中分配,可以改變a的值 如 a[0] = 'a'
char *str = a;<span style="white-space:pre"> </span>// str指針指向 a
<span style="white-space:pre"> </span>std::string str1 = "hello";
<span style="white-space:pre"> </span>str1[0] = 'k';<span style="white-space:pre"> </span>//這裏是可以使用的
<span style="white-space:pre"> </span>char* b = "const value";<span style="white-space:pre"> </span>//const 變量。在常量區分配,其中b指針在棧中,不可以改變值 如b[0] = 'a1';會崩潰
strcpy_s(a, str1.c_str());
<span style="white-space:pre"> </span>//printf("%s\n",str1.c_str());<span style="white-space:pre"> </span>//使用c字符輸出
<span style="white-space:pre"> </span>//std::cout << str1;<span style="white-space:pre"> </span>//這裏使用流輸出字符串
<pre name="code" class="cpp"><span style="white-space:pre"> </span>//從結果可以知道,str指針 和a指針都在在棧中,指向了同一個地址"123456",“123456”也在棧中分配<span> </span>
printf("a:%d str:%d str1:%d\n", &a, &str, &str1);printf("a:%d str:%d str1:%d\n", a, str, str1);
結果:
a:3536824 str:3536812 str1:3536776
a:3536824 str:3536824 str1:8624496
請按任意鍵繼續. . .
請按任意鍵繼續. . .