📄 pointerandstring.html
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<link rel="stylesheet" href="css/stdlayout.css" type="text/css">
<link rel="stylesheet" href="css/print.css" type="text/css">
<meta content="text/html; charset=gb2312" http-equiv="content-type">
<title>指标与字串</title>
</head>
<body>
<h3><a href="http://caterpillar.onlyfun.net/GossipCN/index.html">From
Gossip@caterpillar</a></h3>
<h1><a href="CppGossip.html">C++
Gossip: 指标与字串</a></h1>
字元指标可以参考至一个字串常数,这使得字串的指定相当的方便,例如下面的程式片段宣告一个字串指标,并指向一个字串常数:<br>
<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">char *str = "hello";</span><br>
</div>
<br>
使用字元指标的好处是,您可以直接使用指定运算子将一个字串常数指定给字元指标,例如:<br>
<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">str = "world";</span><br>
</div>
<br>
下面这段程式是个简单的示范:<br>
<br>
<pre>#include <iostream> <br>using namespace std; <br><br>int main() {<br> char *str = "hello"; <br> cout << str << endl; <br><br> str = "world"; <br> cout << str << endl; <br><br> return 0; <br>}<br></pre>
<br>
<span class="postbody">
执行结果:</span><br>
<table style="text-align: left; width: 100%;" border="0" cellpadding="2" cellspacing="2">
<tbody>
<tr>
<td style="background-color: rgb(0, 0, 0);"><small><span style="color: rgb(255, 255, 255);">hello<br>
world</span></small><span style="color: rgb(255, 255, 255);"><br>
</span></td>
</tr>
</tbody>
</table>
<br>
在程式中使用一个字串常数时,该字串常数会占有一个记忆体空间,例如"hello"与"world"都各占有一块记忆体空间,所以上面的程式str前后所
指向的记忆体位址并不相同,下面这个程式可以印证: <br>
<br>
<pre>#include <iostream> <br>using namespace std; <br><br>int main() {<br> char *str = "hello"; <br> void *add = 0; <br><br> add = str; <br> cout << str << "\t" <br> << add << endl; <br><br> str = "world"; <br> add = str; <br> cout << str << "\t" <br> << add << endl; <br> <br> return 0; <br>}</pre>
<span class="postbody"><br>
执行结果:</span><br>
<table style="text-align: left; width: 100%;" border="0" cellpadding="2" cellspacing="2">
<tbody>
<tr>
<td style="background-color: rgb(0, 0, 0);"><small><span style="color: rgb(255, 255, 255);">hello 0x440000<br>
world 0x440008</span></small><span style="color: rgb(255, 255, 255);"><br>
</span></td>
</tr>
</tbody>
</table>
<br>
如上面的程式所示,"hello"字串常数的位址是在0x8048770,而"world"字串常数的位址是在0x804877a,两个的位址并不相同。
<br>
<br>
要注意的是,如果使用阵列的方式宣告字串,则不可以直接使用=指定运算子另外指定字串,例如下面的程式是错误的示范: <br>
<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">char str[] = "hello"; </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">str =
"world"; // error, ISO C++ forbids assignment of arrays </span><br>
</div>
<br>
在字元指标中使用指标阵列,可以更方便的处理字串阵列,例如:<br>
<br>
<pre>#include <iostream> <br>using namespace std; <br><br>int main() {<br> char *str[] = {"professor", "teacher", <br> "student", "etc."}; <br><br> for(int i = 0; i < 4; i++) <br> cout << str[i] << endl; <br> <br> return 0; <br>}<br></pre>
<br>
<span class="postbody">
执行结果:</span><br>
<table style="text-align: left; width: 100%;" border="0" cellpadding="2" cellspacing="2">
<tbody>
<tr>
<td style="background-color: rgb(0, 0, 0);"><small><span style="color: rgb(255, 255, 255);">professor<br>
teacher<br>
student<br>
etc.</span></small><span style="color: rgb(255, 255, 255);"><br>
</span></td>
</tr>
</tbody>
</table>
<br>
str中的每个元素都是字元指标,也各自指向一个字串常数,进一步扩充这个观念,就可以使用二维以上的字串阵列,例如: <br>
<br>
<pre>#include <iostream> <br>using namespace std; <br><br>int main() {<br> char *str[][2] = {"professor", "Justin", <br> "teacher", "Momor", <br> "student", "Caterpillar"}; <br><br> for(int i = 0; i < 3; i++) {<br> cout << str[i][0] << ": " <br> << str[i][1] << endl; <br> }<br><br> return 0; <br>}<br></pre>
<br>
<span class="postbody">
执行结果:</span><br>
<table style="text-align: left; width: 100%;" border="0" cellpadding="2" cellspacing="2">
<tbody>
<tr>
<td style="background-color: rgb(0, 0, 0);"><small><span style="color: rgb(255, 255, 255);">professor: Justin<br>
teacher: Momor<br>
student: Caterpillar</span></small><span style="color: rgb(255, 255, 255);"><br>
</span></td>
</tr>
</tbody>
</table>
<br>
额外补充一点,下面两个宣告的作用虽然类似,但其实意义不同: <br>
<div style="margin-left: 40px; font-family: Courier New,Courier,monospace;"><span style="font-weight: bold;">char *str1[] =
{"professor", "Justin", "etc."}; </span><br style="font-weight: bold;">
<span style="font-weight: bold;">char str2[3][10] =
{"professor", "Justin", "etc."};</span><br>
</div>
<br>
第一个宣告是使用指标阵列,每一个指标元素指向一个字串常数,只要另外指定字串常数给某个指标,该指标指向的记忆体位址就不同了,而第二个宣告则是配置连
续的3x10的字元阵列空间,字串是直接储存在这个空间,每个字串的位址是固定的,而使用的空间也是固定的(也就是含空字元会是10个字元)。 <br>
<br>
<br>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -