C++第六章课后习题.doc
文本预览下载声明
第 六 章 数组、指针与字符串
6-1 数组a[10][5][15]一共有多少个元素?
-2 在数组a[20]中第一个元素和最后一个元素是哪一个?
6-3 用一条语句声明一个有个元素的型数组,并依次赋予1~5的初值。
6-4 已知有一个数组名叫oneArray,用一条语句求出其元素的个数。
6-5 用一条语句一个有5×3个元素的二维型数组,并依次赋予1~15的初值。
6-6 运算符*”和”的作用是什么?
6-7 什么叫做指针?指针中储存的地址和这个地址中的值有何区别?
6-8 声明一个int型指针,用new语句为其分配包含10个元素的地址空间。
6-9 在字符串”Hello,world!”中结束符是什么?
6-10 声明一个有个元素的int型数组,在程序中提示用户输入元素值,最后再在屏幕上显示出来。
#include
using namespace std;
int main int myArray[5];
int i;
for i 0;i 5;i++ cout Value for my Array[ i ]:;
cin myArray[i]; for i 0;i 5;i++ cout i : myArray[i] endl;
return 0; 6-11 引用和指针有何区别?何时只能使用指针而不能使用引用?
6-12 声明下列指针:float类型变量的指针ploat,char类型的指针ptr、struct Customer型的指针p。
Char*pstr;
Struct customer*pcus;
6-13 给定float类型的指针fp,写出显示fp所指向的值的输出流语句。
”Value ” *fp;
6-14 在程序中声明一个double类型变量的指针分别显示指针占了多少字节和指针所指的变量占了多少字节。
#include
using namespace std;
int main double*counter;
cout \nSize of pointer sizeof counter ;
cout endl;
cout nSize of addressed value sizeof *counter ;
return 0; 6-15 const int * p1 和 int * const p2的区别是什么?
6-16 声明一个int型变量a,一个int型指针p,一个引用r,通过p把a的值改为10,通过r把a的值改为5
int a;
int*p a;
int r a;
*p 10;
r 5;
6-17 下列程序有何问题,请仔细体会使用指针时应避免出现这个的问题。
#include
int main int *p; *p 9; cout The value at p: *p; return 0; 解:指针p没有初始化,也就是没有指向某个确定的内存单元,它指向内存中的一个随机地址,给这个随机地址赋值是非常危险的。
6-18 下列程序有何问题,请改正;仔细体会使用指针时应避免出现的这个问题。
#include
int fn1 int * p new int 5 ;
return *p; int main int a fn1 ;
cout the value of a is: a;
return 0; 解:#include
using namespace std;
int*fn1 ;
int main int*a fn1 ;
cout the value of a is: a;
delete a ;
return 0; int *fn1 int*p new int 5 ;
return p; 6-19 声明一个参数为int型,返回值为型的函数指针;声明类A的一个成员函数指针,其参数为int型,返回值型。
Long A::*p_fn2 int ;
6-20 实现一个名为SimpleCircle的简单圆类,其数据成员int*itsRadius为一个指向其半径值的指针,设计对数据成员的各种操作,给出这个类的完实现并测试这个类。
#include
using namespace std;
class SimpleCircle public:
SimpleCircle ;
SimpleCircle int ;
SimpleCircle const SimpleCircle ;
~SimpleCircle void setRadius int ;
int getRadius const;
private:
int*itsRadius;
;
SimpleCircle::SimpleCircl
显示全部