---
title: "站在巨人肩上的思考[连载] （8）"
author: "shi5jin"
date: "2007-12-03T14:58:01Z"
tags: []
categories: ["编程语言"]
slug: "8"
draft: false
lastmod: "2007-12-03T14:58:01Z"
id: 145604
url: "https://shi5jin.iteye.com/blog/145604"
---

### 5.2 数组

#### 5.2.2 字符串文字量

         类似”this is a string.”这样一个字符串文字量，它是一个常量。正如代码中一个显式的数字0xff08一样，其值不可以被更改。如想修改，可以通过复制给一个变量来实现：

         &lt;v:shapetype id="\_x0000\_t202" path="m,l,21600r21600,l21600,xe" o:spt="202" coordsize="21600,21600"&gt;&lt;v:stroke joinstyle="miter"&gt;&lt;/v:stroke&gt;&lt;v:path o:connecttype="rect" gradientshapeok="t"&gt;&lt;/v:path&gt;&lt;/v:shapetype&gt;&lt;v:shape id="\_x0000\_s1034" style="WIDTH: 374.2pt; HEIGHT: 36.05pt; mso-position-horizontal-relative: char; mso-position-vertical-relative: line; mso-width-relative: margin; mso-height-relative: margin" strokeweight="1pt" strokecolor="#95b3d7" type="#\_x0000\_t202"&gt;&lt;v:fill type="gradient" focus="100%" focussize="" focusposition="1" color2="#b8cce4"&gt;&lt;/v:fill&gt;&lt;v:shadow opacity=".5" type="perspective" offset2="-3pt" offset="1pt" color="#243f60" on="t"&gt;&lt;/v:shadow&gt;&lt;v:textbox&gt;

int a = 0xff08;

a = 0xff18;

&lt;/v:textbox&gt;&lt;w:wrap type="none"&gt;&lt;/w:wrap&gt;&lt;w:anchorlock&gt;&lt;/w:anchorlock&gt;&lt;/v:shape&gt;&lt;v:shapetype id="\_x0000\_t75" path="m@4@5l@4@11@9@11@9@5xe" o:spt="75" coordsize="21600,21600" stroked="f" filled="f" o:preferrelative="t"&gt;&lt;v:stroke joinstyle="miter"&gt;&lt;/v:stroke&gt;&lt;v:formulas&gt;&lt;v:f eqn="if lineDrawn pixelLineWidth 0"&gt;&lt;/v:f&gt;&lt;v:f eqn="sum @0 1 0"&gt;&lt;/v:f&gt;&lt;v:f eqn="sum 0 0 @1"&gt;&lt;/v:f&gt;&lt;v:f eqn="prod @2 1 2"&gt;&lt;/v:f&gt;&lt;v:f eqn="prod @3 21600 pixelWidth"&gt;&lt;/v:f&gt;&lt;v:f eqn="prod @3 21600 pixelHeight"&gt;&lt;/v:f&gt;&lt;v:f eqn="sum @0 0 1"&gt;&lt;/v:f&gt;&lt;v:f eqn="prod @6 1 2"&gt;&lt;/v:f&gt;&lt;v:f eqn="prod @7 21600 pixelWidth"&gt;&lt;/v:f&gt;&lt;v:f eqn="sum @8 21600 0"&gt;&lt;/v:f&gt;&lt;v:f eqn="prod @7 21600 pixelHeight"&gt;&lt;/v:f&gt;&lt;v:f eqn="sum @10 21600 0"&gt;&lt;/v:f&gt;&lt;/v:formulas&gt;&lt;v:path o:connecttype="rect" gradientshapeok="t" o:extrusionok="f"&gt;&lt;/v:path&gt;&lt;o:lock aspectratio="t" v:ext="edit"&gt;&lt;/o:lock&gt;&lt;/v:shapetype&gt;

         对于字符串文字量则有一点要注意：

         &lt;v:shape id="\_x0000\_s1033" style="WIDTH: 374.2pt; HEIGHT: 72.85pt; mso-position-horizontal-relative: char; mso-position-vertical-relative: line; mso-width-relative: margin; mso-height-relative: margin" strokeweight="1pt" strokecolor="#95b3d7" type="#\_x0000\_t202"&gt;&lt;v:fill type="gradient" focus="100%" focussize="" focusposition="1" color2="#b8cce4"&gt;&lt;/v:fill&gt;&lt;v:shadow opacity=".5" type="perspective" offset2="-3pt" offset="1pt" color="#243f60" on="t"&gt;&lt;/v:shadow&gt;&lt;v:textbox&gt;

char* cp = “this is a string”;       //危险！赋值给一个普通字符指针；

cp\[4] = ‘e’;                     //未定义行为！不能给常量赋值；

char p\[] = “this is a string”;        //可以；

p\[4] = ‘e’;                      //正确执行；

&lt;/v:textbox&gt;&lt;w:wrap type="none"&gt;&lt;/w:wrap&gt;&lt;w:anchorlock&gt;&lt;/w:anchorlock&gt;&lt;/v:shape&gt;

         可以看出，用char \*来引用字符串文字量是一个十分危险的地方。在上例中cp\[4]=’e’;可以通过编译，但是在执行期间产生了一个Segmentation Fault。也就是说编译器并不能帮助程序员发现这类错误。

我个人是这样认为的：字符串文字量是一中很底层的数据，编译器把他放进汇编语言中的数据段；普通的char\*指针也是一种很底层的指针，他可以指向内存空间中的任何地址；char* cp = “this is a string”;仅仅在栈空间生成一个指针，然后把他指向位于数据段的”this is a string”，初始化并不会导致错误。但是利用cp\[4]更改数据段中的数据内容将会导致段错误，数据段是只读的。而利用char p\[]形式的时候，意味着将在栈空间中生成一个字符数组，然后用”this is a string”来进行初始化，而后p指向这个栈空间中字符数组的首地址。此时p\[4]=’e’;修改的是栈空间中的值，而不是数据段中的值。

但是p是什么类型呢？是char\*么？他的大小是多少？是1么？为了使指针和数组首地址之间的区别更具普遍意义，我们用int类型来测试一下：

&lt;v:shape id="\_x0000\_s1032" style="WIDTH: 374.2pt; HEIGHT: 104.65pt; mso-position-horizontal-relative: char; mso-position-vertical-relative: line; mso-width-relative: margin; mso-height-relative: margin" strokeweight="1pt" strokecolor="#95b3d7" type="#\_x0000\_t202"&gt;&lt;v:fill type="gradient" focus="100%" focussize="" focusposition="1" color2="#b8cce4"&gt;&lt;/v:fill&gt;&lt;v:shadow opacity=".5" type="perspective" offset2="-3pt" offset="1pt" color="#243f60" on="t"&gt;&lt;/v:shadow&gt;&lt;v:textbox&gt;

char p\[] = "hello";

char* ptr = p;

cout &lt;&lt; sizeof(p) &lt;&lt; ":" &lt;&lt; sizeof(\*p) &lt;&lt; endl;

cout &lt;&lt; p &lt;&lt; ":" &lt;&lt; \*p &lt;&lt; endl;

cout &lt;&lt; sizeof(ptr) &lt;&lt; ":" &lt;&lt; sizeof(\*ptr) &lt;&lt; endl;

cout &lt;&lt; ptr &lt;&lt; ":" &lt;&lt; \*ptr &lt;&lt; endl;

&lt;/v:textbox&gt;&lt;w:wrap type="none"&gt;&lt;/w:wrap&gt;&lt;w:anchorlock&gt;&lt;/w:anchorlock&gt;&lt;/v:shape&gt;

这段代码在我的电脑上的输出是：

&lt;v:shape id="\_x0000\_s1031" style="WIDTH: 374.2pt; HEIGHT: 87.2pt; mso-position-horizontal-relative: char; mso-position-vertical-relative: line; mso-width-relative: margin; mso-height-relative: margin" strokeweight="3pt" strokecolor="#f2f2f2 \[3041]" type="#\_x0000\_t202" fillcolor="black \[3200]"&gt;&lt;v:shadow opacity=".5" type="perspective" offset2="-1pt" offset="1pt" color="#7f7f7f \[1601]" on="t"&gt;&lt;/v:shadow&gt;&lt;v:textbox style="mso-next-textbox: #\_x0000\_s1031"&gt;

\[/home/gavin/test]# ./test

6:1

hello:h

4:1

hello:h

&lt;/v:textbox&gt;&lt;w:wrap type="none"&gt;&lt;/w:wrap&gt;&lt;w:anchorlock&gt;&lt;/w:anchorlock&gt;&lt;/v:shape&gt;

ptr是真正的char\*类型，其sizeof是4，但是p的sizeof却是6，也就是”hello”加一个‘\\0’的长度。除此之外，ptr和p表现都一样。但我们最好认为p是字符数组类型，而ptr才是字符指针类型，因为他们的行为上存在差异。很多教师、教材都说：“数组名就是指向数组首地址的指针。”这其实并不完全正确。

再仔细看看，我们cout&lt;&lt;p和cout&lt;&lt;ptr的期望输出应该是p和ptr分别表示的地址。但事实上却是数组的内容。为了明显区分，再看下面这段代码：

&lt;v:shape id="\_x0000\_s1030" style="WIDTH: 374.2pt; HEIGHT: 104.65pt; mso-position-horizontal-relative: char; mso-position-vertical-relative: line; mso-width-relative: margin; mso-height-relative: margin" strokeweight="1pt" strokecolor="#95b3d7" type="#\_x0000\_t202"&gt;&lt;v:fill type="gradient" focus="100%" focussize="" focusposition="1" color2="#b8cce4"&gt;&lt;/v:fill&gt;&lt;v:shadow opacity=".5" type="perspective" offset2="-3pt" offset="1pt" color="#243f60" on="t"&gt;&lt;/v:shadow&gt;&lt;v:textbox&gt;

int arr\[4] = {0};

int* pa = arr;

cout &lt;&lt; sizeof(arr) &lt;&lt; ":" &lt;&lt; sizeof(\*arr) &lt;&lt; endl;

cout &lt;&lt; arr &lt;&lt; ":" &lt;&lt; \*arr &lt;&lt; endl;

cout &lt;&lt; sizeof(pa) &lt;&lt; ":" &lt;&lt; sizeof(\*pa) &lt;&lt; endl;

cout &lt;&lt; pa &lt;&lt; ":" &lt;&lt; \*pa &lt;&lt; endl;

&lt;/v:textbox&gt;&lt;w:wrap type="none"&gt;&lt;/w:wrap&gt;&lt;w:anchorlock&gt;&lt;/w:anchorlock&gt;&lt;/v:shape&gt;

在我的电脑上输出为：

&lt;v:shape id="\_x0000\_s1029" style="WIDTH: 374.2pt; HEIGHT: 87.2pt; mso-position-horizontal-relative: char; mso-position-vertical-relative: line; mso-width-relative: margin; mso-height-relative: margin" strokeweight="3pt" strokecolor="#f2f2f2" type="#\_x0000\_t202" fillcolor="black"&gt;&lt;v:shadow opacity=".5" type="perspective" offset2="-1pt" offset="1pt" color="#7f7f7f" on="t"&gt;&lt;/v:shadow&gt;&lt;v:textbox style="mso-next-textbox: #\_x0000\_s1029"&gt;

\[/home/gavin/test]# ./test

16:4

0xbfc26e5c:0

4:4

0xbfc26e5c:0

&lt;/v:textbox&gt;&lt;w:wrap type="none"&gt;&lt;/w:wrap&gt;&lt;w:anchorlock&gt;&lt;/w:anchorlock&gt;&lt;/v:shape&gt;

sizeof的输出刚才已经说过。这里可以看出，int数组名和char数组名的行为也不一样。事实上，cha数组名与标准类型数组名的行为都不一样。这也就是为什么C风格字符串为许多人诟病的地方。

C++在这方面提供了很好的改进：用一种更上层的类型string来代替C风格字符串。并强烈建议程序员在程序中尽量使用string来代替char \*。这里是你需要做出选择的地方：

&lt;v:shape id="\_x0000\_s1028" style="WIDTH: 374.2pt; HEIGHT: 180.35pt; mso-position-horizontal-relative: char; mso-position-vertical-relative: line; mso-width-relative: margin; mso-height-relative: margin" strokeweight="1pt" strokecolor="#95b3d7" type="#\_x0000\_t202"&gt;&lt;v:fill type="gradient" focus="100%" focussize="" focusposition="1" color2="#b8cce4"&gt;&lt;/v:fill&gt;&lt;v:shadow opacity=".5" type="perspective" offset2="-3pt" offset="1pt" color="#243f60" on="t"&gt;&lt;/v:shadow&gt;&lt;v:textbox&gt;

string str1("1");

string str2("1234567");

string str3("1234567890123456789012345678901234567890");

/*

char str1\[] = "1";

char str2\[] = "1234567";

char str3\[] = "1234567890123456789012345678901234567890";

\*/

cout &lt;&lt; sizeof(str1) &lt;&lt; endl;

cout &lt;&lt; sizeof(str2) &lt;&lt; endl;

cout &lt;&lt; sizeof(str3) &lt;&lt; endl;

&lt;/v:textbox&gt;&lt;w:wrap type="none"&gt;&lt;/w:wrap&gt;&lt;w:anchorlock&gt;&lt;/w:anchorlock&gt;&lt;/v:shape&gt;

当利用C风格字符串的输出为：

&lt;v:shape id="\_x0000\_s1027" style="WIDTH: 374.2pt; HEIGHT: 123pt; mso-position-horizontal-relative: char; mso-position-vertical-relative: line; mso-width-relative: margin; mso-height-relative: margin; mso-left-percent: -10001; mso-top-percent: -10001; mso-position-horizontal: absolute; mso-position-vertical: absolute" strokeweight="3pt" strokecolor="#f2f2f2" type="#\_x0000\_t202" fillcolor="black"&gt;&lt;v:shadow opacity=".5" type="perspective" offset2="-1pt" offset="1pt" color="#7f7f7f" on="t"&gt;&lt;/v:shadow&gt;&lt;v:textbox style="mso-next-textbox: #\_x0000\_s1027"&gt;

\[/home/gavin/test]# ./test

2

8

41

\[/home/gavin/test]# ls -l

-rwxrwxr-x 1 863 863    6559 Dec  3 14:29 test

-rw-rw-r-- 1 863 863     391 Dec  3 14:29 test.cpp

&lt;/v:textbox&gt;&lt;w:wrap type="none"&gt;&lt;/w:wrap&gt;&lt;w:anchorlock&gt;&lt;/w:anchorlock&gt;&lt;/v:shape&gt;

而当使用string时：

&lt;v:shape id="\_x0000\_s1026" style="WIDTH: 374.2pt; HEIGHT: 123pt; mso-position-horizontal-relative: char; mso-position-vertical-relative: line; mso-width-relative: margin; mso-height-relative: margin; mso-left-percent: -10001; mso-top-percent: -10001; mso-position-horizontal: absolute; mso-position-vertical: absolute" strokeweight="3pt" strokecolor="#f2f2f2" type="#\_x0000\_t202" fillcolor="black"&gt;&lt;v:shadow opacity=".5" type="perspective" offset2="-1pt" offset="1pt" color="#7f7f7f" on="t"&gt;&lt;/v:shadow&gt;&lt;v:textbox style="mso-next-textbox: #\_x0000\_s1026"&gt;

\[/home/gavin/test]# ./test

4

4

4

\[/home/gavin/test]# ls -l

-rwxrwxr-x 1 863 863    7578 Dec  3 14:30 test

-rw-rw-r-- 1 863 863     390 Dec  3 14:30 test.cpp

&lt;/v:textbox&gt;&lt;w:wrap type="none"&gt;&lt;/w:wrap&gt;&lt;w:anchorlock&gt;&lt;/w:anchorlock&gt;&lt;/v:shape&gt;

我们也许可以得到如下结论：sting比char\[]需要更少的运行时内存空间，但是char\[]却比string占用更少的磁盘空间。你可以自己选择向左还是向右走。不过相比string的易用性、安全性、以及诸多的成员函数带来的实用功能来说，我个人并不认为char\[]节省的磁盘空间是很突出的优点。