📄 05章 指针与字符串.txt
字号:
软件工程视点5. 1
const限定符可以执行最低权限原则。利用最低权限原则正确设计软件可以大大减少调试时间和不正确的副作用,使程序更容易修改与维护。
可移植性提示5.2
尽管ANSI C和C++中定义了const定符,但有些编译器无法正确实现。
几年来,大量C语言遗留代码都是在没有const限定符的情况下编写的。因此,使用旧版C语言代码的软件工程有很大的改进空间。许多目前使用ANSI C和C++的程序员也没有在程序中使用const限定符,因为他们是从C语言的早期版本开始编程的,这些程序员错过了许多改进软件工程的好机会。
函数参数使用或不用const限定符的可能性有六种,两种用按值调用传递参数,四种按引用调用传递参数,根据最低权限原则来进行选择。在参数中向函数提供完成指定任务所需的数据访问,但不要提供更多权限。
第3章曾经介绍,按值调用传递参数时,函数调用中要生成参数副本并将其传递给函数。如果函数中修改副本,则调用者的原值保持不变。许多情况下,需要修改传入函数的值以使函数能够完成任务。但有时即使被调用函数只是操作原值的副本,也不能在被调用函数中修改这个值。
假设函数取一个单下标数组及其长度为参数,并打印数值。这种函数应对数组进行循环并分别输出每个数组元素。函数体中用数组长度确定数组的最高下标,以便在打印完成后结束循环。在函数体中不能改变这个数组长度。
软件工程视点5.2
如果函数体中不能修改传递的值,则这个参数应声明为const以避免被意外修改。
如果试图修改const类型的值,则编译器会捕获这个错误并发出一个警告或错误消息(取决于特定的编译器)。
轶件工程视点5.3
按值调用时,只舱在调用函数中改变一个值。这个值通过函数返回值进行赋值。要在调用函数中改变多个值,就要按引用传递多个参数。
编程技巧5.3
使用函数之前,检查函数原型以确定可以修改的参数。
将指针传递给函数有四种方法:非常量数据的非常量指针、常量数据的非常量指针、非常量数据的常量指针和常量数据的常量指针。每种组合提供不同的访问权限。
最高访问权限是非常量数据的非常量指针,可以通过复引用指针而修改,指针可以修改成指向其他数据。声明非常量数据的非常量指针时不用const。这种指针可以接收函数中的字符串,用指针算法处理或修改字符串中的每个字符。图5.10中的函数convertToUppercase声明参数sPtr(char*sPtr)为非常量数据的非常量指针。函数用指针算法一次一个字符地处理字符串string。字符串中,st到,x,的字符用函数toupper变为相应的大写字母,其余字符不变。函数toupper取一个字符作为参数。
如果是小写字母,则返回相应的大写字母,否则返回原字符。函数toupper是字符处理库ctype.h中(见第16章)的一部分。
常量数据的非常量指针,指针可以修改成指向其他数据,但数据不能通过指针修改。这种指针可以接收函数的数组参数,函数处理数组每个元素而不修改数据。例如,图5.1l的函数printCharacters将参数sPtr声明为const char*类型.表示“sPtr是字符常量的指针”。函数体用for循环输出字符串中的每个字符,直到遇到null终止符。打印每个字符之后,指针sPtr递增,指向字符串中下一个宇符。
1 // Fig. 5.10: fig0510.cpp
2 // Converting lowercase letters to uppercase letters
3 // using a non-constant pointer to non-constant data
4 #include <iostream.h>
5 #include <ctype.h>
6
7 void convertToUppercase( char * );
8
9 int main()
10 {
11 char string[] = "characters and $32.98";
12
13 cout << "The string before conversion is: "<< string;
14 convertToUppercase( string );
15 cout << "\nThe string after conversion is: "
16 cout << string << endl;
17 return 0;
18 }
19
20 void convertToUppercase{ char *sPtr )
21 {
22 while ( *sPtr != '\0' ) {
23
24 if (*sPtr >= 'a' && *sPtr <= 'z' )
25 *sPtr = toupper( *sPtr ); // convert to uppercase
26
27 ++sPtr; // move sPtr to the next character
28 }
29 }
输出结果:
The string before conversion is: characters and $32.98
The string after conversion is: CHARACTERS AND $32.98
图5.10将字符串变成大写
1 // Fig. 5.11:fig05 ll.cpp
2 // Printing a string one character at a time using
3 // a non-constant pointer to constant data
4 #include <iostream.h>
5
6 void printCharacters( const char * );
7
8 int main()
9 {
10 char string[] = "print characters of a string";
11
12 cout << "The string is:\n";
13 printCharacters( string );
14 cout << endl;
15 return 0;
16 }
17
18 // In printCharacters, sPtr is a pointer to a character
19 // constant. Characters cannot be modified through sPtr
20 // (i.e., sPtr is a "read-only" pointer).
21 void printCharacters( const char *sPtr )
22 {
23 for ( ; *sPtr != '\0'; sPtr++ ) // no initialization
24 cout << *sPtr;
25 }
输出结果:
The string is:
print characters of a string
图5.11 用常量数据的非常量指针打印字符串(一次打印一个字符)
图5.12演示了函数接收常量数据的非常量指针,并试图通过指针修改数据在编译时产生的语法错误消息。
1 // Fig. 5.12: fig05_12.cpp
2 // Attempting to modify data through a
3 // non-constant pointer to constant data.
4 #include <iostream.h>
5
6 void f( const int* );
7
8 int main()
9 {
10 int y;
11
12 f( &y ); // f attempts illegak modification
13
14 return 0;
15 }
16
17 // In f, xPtr is a poin er to an integer constant
18 void f( const int *xPtr )
19 {
20 *xPtr = 100; // cannot modify a const object
21 }
输出结果:
Compiling FIG05 12.CPP:
Error FIG05 12.CPP 20: Cannot modify a const object
Warning FIGOS_12.CPP 21: Parameter 'xPtr' is never used
图5.12 试图通过常量数据的非常量指针修改数据
众所周知,数组是累计数据类型,用同一名称存放相同类型的相关数据项。第6章将介绍另一种形式的累计数据类型——结构(structure),也称为记录(record)。结构可以用同一名称存放不同类型的相关数据项(例如,存放公司每个员工的信息)。调用带数组参数的函数时,数组模拟按引用调用自动传递给函数。但结构则总是按值调用,传递整个结构的副本。这就需要复制结构中每个数据项目并将其存放在计算机函数调用堆栈中的执行时开销(函数执行时用函数调用堆栈存放函数调用中使用的局部变量)。结构数据要传递绐函数时,可以用常量数据的指针(或常量数据的引用)得到按引用调用的性能和按值调用对数据的保护。传递结构的指针时,只要复制存放结构的地址。在4字节地址的机器上,只要复制4字节内存而不是复制结构的几百或几千字节。
性能提示5.7
要传递结构之类的大对象时,可以用常量数据的指什(或常量数据的引用)得到按引用调用的性能和按值调用对数据的保护。
非常量数据的常量指针总是指向相同的内存地址,该地址中的数据可以通过指针修改。这里的数组名是默认的。数组名是数组开头的常量指针,数组中的所有数据可以用数组名和数组下标访问和修改。非常量数据的常量指针可以接收数组为函数参数,该函数只用数组下标符号访问数组元素。声明为const的指针应在声明时初始化(如果是函数参数,则用传入函数的指针初始化)。图5.13的程序想修改常量指针,指针ptr的类型声明为int *const,图中的声明表示“ptr是整数的常量指针”,指针用整型变量x的地址初始化。程序要将y的地址赋给ptr,但产生一个错误消息。注意数值7赋
给*ptr时不产生错误,说明ptr所指的值是可修改的。
常见编程错误5.6
声明为const的指针不在声明时初始化是个语法错误。
常量数据的常量指针的访问权限最低。这种指针总是指向相同的内存地址,该内存地址的数据不能修改。数组传递到函数中,该函数只用数组下标符号读取,而不能修改数组。图5.14的程序演示声明指针变量ptr为const int* const,表示“ptr是常量整数的常量指针”。图中显示了修改ptr所指数据和修改存放指针变量的地址时产生的错误消息。注意输出ptr所指的值时不产生错误,因为输出语句中没有进行修改。
1 // Fig. 5.13:fig05 13.cpp
2 // Attempting to modify a constant pointer to
3 // non-constant data
4 #include <iostream.h>
5
6 int main()
7 {
8 int x, y;
9
10 int * const ptr = &x; // ptr is a constant pointer to an
11 // integer. An integer can be modified
12 // through ptr, but ptr always points
13 // to the same memory location.
14 *ptr = 7;
15 ptr = &y;
16
17 return 0;
18 }
输出结果:
Compiling FIG05 13.CPP:
Error FIG05 13.CPP 15: Cannot modify a const object
Warning FIGOS_13.CPP 18: 'y' is declared but never used
图5.13修改非常量数据的常量指针
1 // Fig. 5.141 fig05 14.cpp
2 // Attempting to modify a constant pointer to
3 // constant data.
4 #include <iostream.h>
5
6 int main()
7 {
8 int x = 5, y;
9
10 const iht *const ptr = &x; // ptr is a constant pointer to a
11 // constant integer, ptr always
12 // points to the same location
13 // and the integer at that
14 // location cannot be modified.
15 eout << *ptr << endl;
16 *ptr = 7;
17 ptr = &y;
18
19 return 0;
20 }
输出结果:
Compiling FIG05 14.CPP:
Error FIG05_14.CPP 16: Cannot modify a const object
Error FIG05_14.CPP 17: Cannot modify a const object
Warning FIG05_14.CPP 20: 'y' is declared but never used
图5.14 修改常量数据的常量指针
5.6 按引用调用的冒泡排序
下面将图4.16的冒泡排序程序修改成用两个函数bubbleSort和swap(如图5.15)。函数bubbleSort进行数组排序,它调用函数swap,变换数组元素array[j)和array[j+1]记住,C++强制函数之间的信息隐藏,因此swap并不能访问bubbleSort中的各个元素。由于bubbleSort要求swap访问交换的数组元素,因此bubbleSort要将这些元素按引用调用传递给swap.每个数组元素的地址显式传递。
尽管整个数组自动按引用调用传递,但各个数组元素是标量,通常按值调用传递。因此,bubbleSort对swap调用中的每个数组元素使用地址运算符(&),如下所示的语句:
swap( &array[ j ], array[j+ 1]);
实现按引用调用。函数swap用指针变量element1Ptr接收&array[j]。由于信息隐藏,swap并不知道名称&array[j],但swap可以用*element1Ptr作为&array[j]的同义词。这样,swap引用*element1Ptr时,实际上是引用bubbleSort中的&array[j]。同样,swap引用*element2Ptr时,实际上是引用bubbleSort中的array[j+1]。虽然swap不能用:
hold = array [ j ];
array[ j ] = array[ j + 1 ];
array[ j + 1 ] = hold;
但图5.15中的swaP函数用
hold = * element1Ptr;
*element1Ptr = *element2Ptr;
*element2Ptr = hold;
达到相同的效果。
1 // Fig. 5.15: fig05_15.cpp
2 // This program puts values into an array, sorts the values into
3 // ascending order, and prints the resulting array.
4 #include <iostream.h>
5 #include <iomanip.h>
6
7 void bubbleSort{ int *, const int );
8
D int main(}
l0 {
11 const int arraySize = 10;
12 int a[ arraySize ) = { 2, 6, 4, 8, 10, 12, 89, 68, 45, 37 };
13 int i;
14
15 cout << "Data items in original order\n";
16
17 for ( i = 0; t < arraySize; i++ )
18 cout << setw( 4 ) << a[ i ];
19
20 bubbleSort( a, arraySize ); // sort the array
21 cout << "\nData items inascending order\n";
22
23 for ( i = 0; i < arraySize; i++ )
24 cout << setw( 4 ) << a[ i ];
25
26 cout << endl;
27 return 0;
28 }
29
30 void bubbleSort( int *array, const int size )
31 {
32 void swap( int *, iht * );
33
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -