1.char s[]="";
sizeof(s)=1;
三、简答题
1、(1)头文件中的ifndef/define/endif的作用
答:防止头文件重复引用
(2)#include<filename.h>和#include“filename.h”的区别
答:)#include<filename.h>编译器从开发环境设置的路径开始搜索filename.h;#include“filename.h”
编译器从用户工作目录开始搜索。
2、const的用途(至少说出两种)
答:(1)定义const常量
(2)修饰函数的参数、返回值、函数的定义体,被const修饰的东西受到强制保护,防止被意外修改,提高程序的健壮性。
3、在c++程序中调用被c编译器编译后的函数,为是么要加extern “c”
答:c++支持函数重载,c不支持函数重载。函数被c++编译在库中的名字和c语言不同,因此,c++提供了连接交换指定符号extern来解决名字匹配问题。
4、简述以下两个for循环的优缺点
//第一个 //第二个
for(i=0;i<N;i++) if(condition)
{ {
if(condition) for(i=0;i<N;i++)
DoSomething(); DoDoSomething();
else }
DoOtherthing(); else
} { for(i=0;i<N;i++)
DoOtherthing();
}
优点:程序简洁 优点:循环效率高。
缺点:多执行了N-1次逻辑判断,并且打断了 缺点:程序不简洁。
循环“流水线”作业,使得编译器不能对循环
进行优化处理,降低了效率。
四、有关内存的思考题
运行Test函数会有事么结果,为是么?
1、void GetMemory1(char *p,int num)
{
*p=(char *)malloc(sizeof(char)*num);
}
void Test(void)
{ char *str=NULL;
GetMemory1(str);
strcpy(str,"hello world");
printf(str);
}
答:程序崩溃。GetMemory不能返回动态内存,test里面的str一直为NULL,strcpy将使程序崩溃。
2、char *GetMemory2(void)
{
char p[]="hello world!";
return p;
}
void Test(void)
{ char *str=NULL;
str=GetMemory2();
printf(str);
}
答:可能是乱码。GetMemory返回的是指向“栈内存”指针,地址不为NULL,但返回后原有的内容已被清空,新内容不可知
3、void GetMemory3(char **p,int num)
{
*p=(char *)malloc(sizeof(char)*num);
}
void Test(void)
{ char *str=NULL;
GetMemory3(&str,100);
strcpy(str,"hello");
printf(str);
}
答:输出hello。但存在内存泄漏。
4、void Test(void)
{ char *str=(char *)malloc(100);
strcpy(str,"hello");
free(str);
if(str!=NULL) {
strcpy(str,"world");
printf("%s\n",str);
}
}
答:输出hello,但篡改动态内存的内容,后果难以预料,非常危险。因free之后,str指针成为野指针,if(str!=NULL)语句不再起作用。
五、类型转换
double d=100.25;
int x=d;
int *pInt=(int *)&d;
请问以下两个输出语句的结果是否相同,为是么?
cout<<x<<endl;
cout<<*pInt<<endl;
答:(1)输出100,x取d的整数部分。(2)输出的不是100,*pInt指向的的前4个字节的数值,而不是d的整数部分。
六、编写strrcpy函数。已知原型为char *strcpy(char *strDest,const char *strSrc);
(1)不调用字符串的库函数,实现strcpy
(2)strcpy能把strSrc的内容复制到strDest,为何还要char *类型的返回值?
答:
(1)char *strcpy(char *strDest,const char *strSrc)
{ assert((strDest!=NULL)&&(strSrc!=NULL));//judge wether the two pointers are NULL
char *address=strDest;
while((*strDest++=*strSrc++)!='\0') NULL;
return address;
}
(2)为了实现链式表达
七、编写string类的构造函数、析构函数和赋值函数
类string原型
class string
{ public:
string(const char *str=NULL);//普通构造函数
string(const string &other);//拷贝构造函数
~string(void);
string &operate=(const string &other);//赋值函数
private:
char *m_data;
}
string::string(const char *str))//普通构造函数
{ if(str==NULL)
{ m_data=new char[1];
m_data='\0';
}
else
{ m_data=new char[strlen(str)+1];
strcpy(m_data,str);
}
}
string(const string &other)//拷贝构造函数
{ m_data=new char[strlen(other.m_data)+1];
strcpy(m_data,other.m_data);
}
string &string::operate=(const string&other)//赋值函数
{ if(this==&other)//(1)检查自赋值
return *this;
delete []m_data //(2)释放原有空间
m_data=new char[strlen(other.m_data)+1];//(3)重新分配空间并赋值
strcpy(m_data,other.m_data);
return *this;//(4)返回本对象的引用
}
~string::string(void)
{ delete []m_data;
}
![]() |
姚明赛场竟遭对手“强吻”(图)
王治郅:篮球不会有太多奇迹存在
印度铜牌得主获奖300万
|
![]() |
组图:霍家否认迎娶郭晶晶 组图:诧异!北京奥运会十大意外 |
![]() |
![]() |
![]() |


档案
日志
相册
视频








评论
想第一时间抢沙发么?