This page looks plain and unstyled because you're using a non-standard compliant browser. To see it in its best form, please upgrade to a browser that supports web standards. It's free and painless.
| « | 七月 2009 | » | ||||
|---|---|---|---|---|---|---|
| 一 | 二 | 三 | 四 | 五 | 六 | 日 |
| 1 | 2 | 3 | 4 | 5 | ||
| 6 | 7 | 8 | 9 | 10 | 11 | 12 |
| 13 | 14 | 15 | 16 | 17 | 18 | 19 |
| 20 | 21 | 22 | 23 | 24 | 25 | 26 |
| 27 | 28 | 29 | 30 | 31 | ||
出口香烟
FC 4下安装ORACLE 9i 的全过程
安装Linux 的硬盘分区
C语言面试题大汇总之华为面试题
面试经典试题
C/C++ 程序设计员应聘常见面试试题深入剖析
美国军工五巨头简介
经典面试问题回答思路
老师的语录
华为公司 java 面试题
本文的写作目的并不在于提供C/C++程序员求职面试指导,而旨在从技术上分析面试题的内涵。文中的大多数面试题来自各大论坛,部分试题解答也参考了网友的意见。
许多面试题看似简单,却需要深厚的基本功才能给出完美的解答。企业要求面试者写一个最简单的strcpy函数都可看出面试者在技术上究竟达到了怎样的程
度,我们能真正写好一个strcpy函数吗?我们都觉得自己能,可是我们写出的strcpy很可能只能拿到10分中的2分。读者可从本文看到strcpy
函数从2分到10分解答的例子,看看自己属于什么样的层次。此外,还有一些面试题考查面试者敏捷的思维能力。
分析这些面试题,本身包含很强的趣味性;而作为一名研发人员,通过对这些面试题的深入剖析则可进一步增强自身的内功。
2.找错题
试题1:
| void test1() { char string[10]; char* str1 = "0123456789"; strcpy( string, str1 ); } |
| void test2() { char string[10], str1[10]; int i; for(i=0; i<10; i++) { str1[i] = 'a'; } strcpy( string, str1 ); } |
| void test3(char* str1) { char string[10]; if( strlen( str1 ) <= 10 ) { strcpy( string, str1 ); } } |
| void strcpy( char *strDest, char *strSrc ) { while( (*strDest++ = * strSrc++) != ‘’ ); } |
| void strcpy( char *strDest, const char *strSrc ) //将源字符串加const,表明其为输入参数,加2分 { while( (*strDest++ = * strSrc++) != ‘’ ); } |
| void strcpy(char *strDest, const char *strSrc) { //对源地址和目的地址加非0断言,加3分 assert( (strDest != NULL) && (strSrc != NULL) ); while( (*strDest++ = * strSrc++) != ‘’ ); } |
| //为了实现链式操作,将目的地址返回,加3分! char * strcpy( char *strDest, const char *strSrc ) { assert( (strDest != NULL) && (strSrc != NULL) ); char *address = strDest; while( (*strDest++ = * strSrc++) != ‘’ ); return address; } |
| { assert( strt != NULL ); //断言字符串地址非0 int len; while( (*str++) != '' ) { len++; } return len; } |
| void GetMemory( char *p ) { p = (char *) malloc( 100 ); } void Test( void ) { char *str = NULL; GetMemory( str ); strcpy( str, "hello world" ); printf( str ); } |
| char *GetMemory( void ) { char p[] = "hello world"; return p; } void Test( void ) { char *str = NULL; str = GetMemory(); printf( str ); } |
| void GetMemory( char **p, int num ) { *p = (char *) malloc( num ); } void Test( void ) { char *str = NULL; GetMemory( &str, 100 ); strcpy( str, "hello" ); printf( str ); } |
| void Test( void ) { char *str = (char *) malloc( 100 ); strcpy( str, "hello" ); free( str ); ... //省略的其它语句 } |
| char *str = NULL; GetMemory( str ); |
| char p[] = "hello world"; return p; |
| *p = (char *) malloc( num ); |
| if ( *p == NULL ) { ...//进行申请内存失败处理 } |
| char *str = (char *) malloc(100); |
| str = NULL; |
| swap( int* p1,int* p2 ) { int *p; *p = *p1; *p1 = *p2; *p2 = *p; } |
| swap( int* p1,int* p2 ) { int p; p = *p1; *p1 = *p2; *p2 = p; } |
| | |
| void Func ( char str[100] ) { sizeof( str ) = ? } void *p = malloc( 100 ); sizeof ( p ) = ? |
| sizeof( str ) = 4 sizeof ( p ) = 4 |
| char str[10]; cout << sizeof(str) << endl; |
| char str[10]; str++; //编译出错,提示str不是左值 |
| least = MIN(*p++, b); |
| #define MIN(A,B) ((A) <= (B) ? (A) : (B)) |
| #define MIN(A,B) (A) <= (B) ? (A) : (B) #define MIN(A,B) (A <= B ? A : B ) |
| #define MIN(A,B) ((A) <= (B) ? (A) : (B)); |
| #ifndef __INCvxWorksh #define __INCvxWorksh #ifdef __cplusplus extern "C" { #endif /*...*/ #ifdef __cplusplus } #endif #endif /* __INCvxWorksh */ |
| #ifndef __INCvxWorksh #define __INCvxWorksh #endif |
| void foo(int x, int y); |
该函数被C编译器编译后在symbol库中的名字为_foo,而C++编译器则会产生像_foo_int_int之类的名字。_foo_int_int这样的名字包含了函数名和函数参数数量及类型信息,C++就是考这种机制来实现函数重载的。
为了实现C和C++的混合编程,C++提供了C连接交换指定符号extern "C"来解决名字匹配问题,函数声明前加上extern "C"后,则编译器就会按照C语言的方式将该函数编译为_foo,这样C语言中就可以调用C++的函数了。
试题5:编写一个函数,作用是把一个char组成的字符串循环右移n个。比如原来是“abcdefghi”如果n=2,移位后应该是“hiabcdefgh”
函数头是这样的:
| //pStr是指向以''结尾的字符串的指针 //steps是要求移动的n void LoopMove ( char * pStr, int steps ) { //请填充... } |
| void LoopMove ( char *pStr, int steps ) { int n = strlen( pStr ) - steps; char tmp[MAX_LEN]; strcpy ( tmp, pStr + n ); strcpy ( tmp + steps, pStr); *( tmp + strlen ( pStr ) ) = ''; strcpy( pStr, tmp ); } |
| void LoopMove ( char *pStr, int steps ) { int n = strlen( pStr ) - steps; char tmp[MAX_LEN]; memcpy( tmp, pStr + n, steps ); memcpy(pStr + steps, pStr, n ); memcpy(pStr, tmp, steps ); } |
|
| 偏移地址 | 字节数 | 数据类型 | 内 容 |
| 文件头
| 00H | 4 | Char | "RIFF"标志 |
| 04H | 4 | int32 | 文件长度 | |
| 08H | 4 | Char | "WAVE"标志 | |
| 0CH | 4 | Char | "fmt"标志 | |
| 10H | 4 | 过渡字节(不定) | ||
| 14H | 2 | int16 | 格式类别 | |
| 16H | 2 | int16 | 通道数 | |
| 18H | 2 | int16 | 采样率(每秒样本数),表示每个通道的播放速度 | |
| 1CH | 4 | int32 | 波形音频数据传送速率 | |
| 20H | 2 | int16 | 数据块的调整数(按字节算的) | |
| 22H | 2 | 每样本的数据位数 | ||
| 24H | 4 | Char | 数据标记符"data" | |
| 28H | 4 | int32 | 语音数据的长度 |
| typedef struct tagWaveFormat { char cRiffFlag[4]; UIN32 nFileLen; char cWaveFlag[4]; char cFmtFlag[4]; char cTransition[4]; UIN16 nFormatTag ; UIN16 nChannels; UIN16 nSamplesPerSec; UIN32 nAvgBytesperSec; UIN16 nBlockAlign; UIN16 nBitNumPerSample; char cDataFlag[4]; UIN16 nAudioLength; } WAVEFORMAT; |
| WAVEFORMAT waveFormat; memcpy( &waveFormat, buffer,sizeof( WAVEFORMAT ) ); |
| 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加NULL 判断 *m_data = ''; } else { int length = strlen(str); m_data = new char[length+1]; // 若能加 NULL 判断则更好 strcpy(m_data, str); } } // String的析构函数 String::~String(void) { delete [] m_data; // 或delete m_data; } //拷贝构造函数 String::String(const String &other) // 得分点:输入参数为const型 { int length = strlen(other.m_data); m_data = new char[length+1]; //加分点:对m_data加NULL 判断 strcpy(m_data, other.m_data); } //赋值函数 String & String::operate =(const String &other) // 得分点:输入参数为const型 { if(this == &other) //得分点:检查自赋值 return *this; delete [] m_data; //得分点:释放原有的内存资源 int length = strlen( other.m_data ); m_data = new char[length+1]; //加分点:对m_data加NULL 判断 strcpy( m_data, other.m_data ); return *this; //得分点:返回本对象的引用 } |
| | |
| const classA operator*(const classA& a1,const classA& a2); |
| classA a, b, c; (a * b) = c; // 对a*b的结果赋值 |
| int checkCPU() { { union w { int a; char b; } c; c.a = 1; return (c.b == 1); } } |
| 内存地址 | 存放内容 |
| 0x4000 | 0x34 |
| 0x4001 | 0x12 |
| 内存地址 | 存放内容 |
| 0x4000 | 0x12 |
| 0x4001 | 0x34 |
| 内存地址 | 存放内容 |
| 0x4000 | 0x78 |
| 0x4001 | 0x56 |
| 0x4002 | 0x34 |
| 0x4003 | 0x12 |
| 内存地址 | 存放内容 |
| 0x4000 | 0x12 |
| 0x4001 | 0x34 |
| 0x4002 | 0x56 |
| 0x4003 | 0x78 |
| int Sum( int n ) { return ( (long)1 + n) * n / 2; //或return (1l + n) * n / 2; } |
| int Sum( int n ) { long sum = 0; for( int i=1; i<=n; i++ ) { sum += i; } return sum; } |