华为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”
相关文档:
这篇文章是使用SQLite C/C++接口的一个概要介绍和入门指南。
由于早期的SQLite只支持5个C/C++接口,因而非常容易学习和使用,但是随着SQLite功能的增强,新的C/C++接口不断的增加进来,到现在有超过150个不同的API接口。这往往使初学者望而却步。幸运的是,大多数SQLite中的C/C++接口是专用的,因而很少被使用到。尽管有这 ......
#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<< ......
(转)C++中extern “C”含义深层探索
1.引言
C++语言的创建初衷是“a better C”,但是这并不意味着C++中类似C语言的全局变量和函数所采用的编译和连接方式与C语言完全相同。作为一种欲与C兼容的语言,C++保留了一部分过程式语言的特点(被世人称为“不彻底地面向对象&rdquo ......
1.下面哪种代码风格更好,why?
A . if ('A' == a)
{a++;}
B. if( a == 'A')
{a++;}
答案:A,如果把==错写成=,因为编译器不允许对常量赋值,容易差错。
2.#define MUTI(x) (x*x)
int i=3,j, ......
#include<stdio.h>
#include<stdarg.h>
#include<string.h>
void demo(char *msg,...)
{
va_list argp;
int arg_number=0;
char *para = msg;
va_start(argp,msg);
while(1){
if ( strcmp( para, "\0") != 0 ) {
arg_number++;
printf("parameter %d is: %s\n",arg_number,p ......