华为C/C++笔试题2
华为C/C++笔试题2 收藏
1. 某32位系统下, C++程序,请计算sizeof 的值
#include <stdio.h>
#include <malloc.h>
void Foo ( char str[100] )
{
printf("sizeof(str)=%d \n", sizeof(str) );//此处使用char *str与char str[100]是一样的,char str[100]不指明大小(char str[])也行,因为编译器是把它当做 一个指针来处理的
}
main()
{
char str[] = "www.ibegroup.com";
char *p1 = str ;
int n = 10;
void *p2 = malloc( 100 );
printf("sizeof(str)=%d \n", sizeof(str) );
printf("sizeof(p1)=%d \n", sizeof(p1) );
printf("sizeof(n)=%d \n", sizeof(n) );
printf("sizeof(p2)=%d \n", sizeof(p2) );
Foo(str);//数组名相当于一个指针,指针的大小为4,所以输出4而不是17,另外sizeof 是计算类型长度的,strlen 才是计算字符串长度的}
}
答:(1)17 (2)4 (3) 4 (4)4 (5)4
2. 回答下面的问题
(1) 头文件中的 ifndef/define/endif干什么用? 预处理
答:防止头文件被重复引用
(2) #include <filename.h> 和 #include "filename.h" 有什么区别?
答:
对于#include <filename.h> ,编译器从标准库路径开始搜索filename.h
对于#include "filename.h" ,编译器从用户的工作路径开始搜索filename.h
(3) 在C++ 程序中调用被 C 编译器编译后的函数,为什么要加 extern “C”声明?
答:函数和变量被C++编译后在符号库中的名字与C语言的不同,被extern "C"修饰的变量和函数是按照C语言方式编译和连接的。由于编译后的名字不同,C++程序不能直接调用C 函数。C++提供了一个C 连接交换指定符号extern“C”来解决这个问题。
3. 回答下面的问题
(1) 请问运行Test 函数会有什么样的结果?
Void GetMemory(char **p, int num)
{
*p = (char *)malloc(num);
}
void Test(void)
{
char *str = NULL;
GetMemory(&str, 100);
strcpy(str, "hello");
printf(str);
}
答:输出“hello”
相关文档:
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include ".\sqlite3_lib\sqlite3.h"
static int _callback_exec(void * notused,int argc, char ** argv, char ** aszColName)
{
int i;
for ( i=0; i<argc; i++ )
......
#include "stdafx.h"
#include <iostream>
#include <cmath>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
double a,b,c;
double delta;
double x1,x2;
cout<<"Please input a,b,c:"<<endl;
cin>>a>>b>>c;
if(cin.fail())
{
cout<< ......
1)a = a + 5; 与 a += 5;的区别。
二者在广义上是等价。D.Ritchie 在C语言中引入复合运算符的主要目的是为了提高编译的效率以产生高质量的执行代码。因为这些运算符的功能基本上都能用一二条机器指令来完成。
2)在C++中long 与 int 的区别
NameDescriptionSize*Range*
char
Character or s ......
在C/C++中,跳出多层循环有3中方法:
1.用break;加上一个辅助的标志变量。
2.用goto;
3.用try ... catch;
其中break对if-else语句无效,每次使用只能跳出一层循环。
用break的具体方法为:
bool BREAK=false;
while(...){
for(...){
......