C/C++笔试题目常见题目
而结构的所有成员都存在(不同成员的存放地址不同)。
2. 对于联合的不同成员赋值, 将会对其它成员重写, 原来成员的值就不存在了, 而对于结构的不同成
员赋值是互不影响的。
10. 下面关于“联合”的题目的输出?
a)
#i nclude <stdio.h>
union
{
int i;
char x[2];
}a;
void main()
{
a.x[0] = 10;
a.x[1] = 1;
printf("%d",a.i);
}
答案:266 (低位低地址,高位高地址,内存占用情况是Ox010A)
b)
main()
{
union{ /*定义一个联合*/
int i;
struct{ /*在联合中定义一个结构*/
char first;
char second;
}half;
}number;
number.i=0x4241; /*联合成员赋值*/
printf("%c%c\n", number.half.first, mumber.half.second);
number.half.first='a'; /*联合中结构成员赋值*/
number.half.second='b';
printf("%x\n", number.i);
getch();
}
答案: AB (0x41对应'A',是低位;Ox42对应'B',是高位)
6261 (number.i和number.half共用一块地址空间)
11. 已知strcpy的函数原型:char *strcpy(char *strDest, const char *strSrc)其中strDest
是目的字符串,strSrc 是源字符串。不调用C++/C 的字符串库函数,请编写函数 strcpy。
答案:
char *strcpy(char *strDest, const char *strSrc)
{
if ( strDest == NULL || strSrc == NULL)
return NULL ;
if ( strDest == strSrc)
return strDest ;
char *tempptr = strDest ;
while( (*strDest++ = *strSrc++) != ‘\0’)
return tempptr ;
}
12. 已知String类定义如下:
class String
{
public:
String(const char *str = NULL); // 通用构造函数
String(const String &another); // 拷贝构造函数
~ String(); // 析构函数
String & operater =(const String &rhs); // 赋值函数
private:
char *m_data; // 用于保存字符串
};
尝试写出类的成员函数实现。
答案:
String::String(const char *str)
{
if ( str == NULL ) //strlen在参数为NULL时会抛异常才会有这步判断
{
m_data = new char[1] ;
m_data[0] = '\0' ;
}
else
{
m_data = new char[strlen(str) + 1];
strcpy(m_data,str);
}
}
String::String(const String &another)
{
m_data = new char[strlen(another.m_data) + 1];
strcpy(m_data,other.m_data);
}
String& String::operator =(const String &rhs)
{
if ( this == &rhs)
return *this ;
delete []m_data; //删除原来的数据,新开一块内存
m_data = new char[strlen(rhs.m_data) + 1];
strcpy(m_data,rhs.m_data);
return *this ;
}
String::~String()
{
delete []m_data ;
}
13. .h头文件中的ifndef/define/endif 的作用?
答:防止该头文件被重复引用。
14. #i nclude<file.h> 与 #i nclude "file.h"的区别?
答:前者是从Standard Library的路径寻找和引用file.h,而后者是从当前工作路径搜寻并引用file.h
。
15.在C++ 程序中调用被C 编译器编译后的函数,为什么要加extern “C”?
首先,作为extern是C/C++语言中表明函数和全局变量作用范围(可见性)的关键字,该关键字告诉编
译器,其声明的函数和变量可以在本模块或其它模块中使用。
通常,在模块的头文件中对本模块提供给其它模块引用的函数和全局变量以关键字extern声明。例如,
上一页 [1] [2] [3] [4] [5] [6] 下一页
- 上一篇:百度公司的笔试题目
《C/C++笔试题目常见题目》相关文章
- C/C++笔试题目常见题目
- › 应聘职位名称 —— Accounting and Finance(会计与财务部分)
- › ACCOUNTANT(General)
- › An accounting position about financial
- › Resume accounts 会计行业英文简历应届生
- › 英文简历2--accounting-cl.doc
- › 英文简历(会计师)ACCOUNTANT(General)
- › 会计师英文简历ACCOUNTANT(General)
- › 专家写的英文简历模板ACCOUNTANT(General)
- › 会计人员英文简历Accounting Manager
- › 会计Accountant笔试题
- › cicc MRM 笔经
- › 埃森哲accenture笔试题型
- 在百度中搜索相关文章:C/C++笔试题目常见题目
- 在谷歌中搜索相关文章:C/C++笔试题目常见题目
- 在soso中搜索相关文章:C/C++笔试题目常见题目
- 在搜狗中搜索相关文章:C/C++笔试题目常见题目