一、请填写BOOL, float, 指针变量与“零值”比较的if语句。(10分)
提示:这里“零值”可以是0, 0.0, FALSE或者“空指针”。例如 int 变量 n 与“零值”比较的 if 语句为:
if (n == 0)
if (n != 0)
以此类推。
请写出 BOOL flag 与“零值”比较的 if 语句: |
请写出 float x 与“零值”比较的 if 语句: |
请写出 char *p 与“零值”比较的 if 语句: |
二、以下为Linux下的32位C++程序,4字节对齐,请计算sizeof的值(10分)
2.1 char str[] = “Hello”; char *p = str; int n = 10; typedef union {long i; int k[5]; char c;} DATE; DATE max; struct name1{ char str; short x; int num; } struct name2{ char str; int num; short x; } 请计算 sizeof(str) = ? sizeof(p) = ? sizeof(n) = ? sizeof(max)= ? sizeof(struct name1)=? sizeof(struct name2)=? | 2.2 Void Func (char str[100]) { 请计算 sizeof(str) = ? } |
2.3 void *p = malloc(100); 请计算sizeof(p) = ? |
三、简答题
1、头文件中的 #ifndef/#define/#endif 有什么用处?
2、#include <filename.h>和 #include “filename.h” 有什么区别?
3、堆栈溢出一般是由什么原因导致的?
4、static全局变量与普通的全局变量有什么区别?static局部变量和普通局部变量有什么区别?static函数与普通函数有什么区别?
5、队列和栈有什么区别?
6、进程和线程的差别。
7、Heap与stack的差别。
10、数组和链表的区别
11、请写出下列代码的输出内容
main()
{
int a,b,c,d;
a=10;
b=a++;
c=++a;
d=10*a++;
printf("b,c,d:%d,%d,%d",b,c,d);
return 0;
}
12.unsigned char *p1;
unsigned long *p2;
p1=(unsigned char *)0x801000;
p2=(unsigned long *)0x810000;
p1+5= ?
p2+5= ?
13.
char * const p;
char const *p;
const char *p;
上述三种定义有什么区别?
四、改错题
4.1 void GetMemory(char *p) { p = (char *)malloc(100); } void Test(void) { char *str = NULL; GetMemory(str); strcpy(str, "hello world"); printf(str); } 请问运行Test函数会有什么样的结果? | 4.2 char *GetMemory(void) { char p[] = "hello world"; return p; } void Test(void) { char *str = NULL; str = GetMemory(); printf(str); } 请问运行Test函数会有什么样的结果? | ||
4.3 Void GetMemory2(char **p, int num) { *p = (char *)malloc(num); } void Test(void) { char *str = NULL; GetMemory(&str, 100); strcpy(str, "hello"); printf(str); } 请问运行Test函数会有什么样的结果? | 4.4 void Test(void) { char *str = (char *) malloc(100); strcpy(str, “hello”); free(str); if(str != NULL) { strcpy(str, “world”); printf(str); } } 请问运行Test函数会有什么样的结果? | ||
4.5 请找出下面代码中的所有错误。 说明:以下代码是把一个字符串倒序,如“abcd”倒序后变为“dcba” #include"string.h" main() { char*src="hello,world"; char* dest=NULL; int len=strlen(src); dest=(char*)malloc(len); char* d=dest; char* s=src[len]; while(len--!=0) d++=s--; *d = 0; printf("%s",dest); free(dest); return 0; } | 4.6 下面的函数实现在一个数上加一个数,有什么错误? int add_n(int n) { static int i = 100; i += n; return i; } | ||
4.7 下面这个程序执行后会有什么错误: int main() { unsigned char A[MAX],i; for (unsigned char i=0;i<=MAX;i++) A[i]=i; } |
五、编写strcpy函数(10分)
已知strcpy函数的原型是char *strcpy(char *strDest, const char *strSrc);其中strDest是目的字符串,strSrc是源字符串。
不调用C++/C的字符串库函数,请编写函数strcpy。
六、实现x是否为2的若干次幂的判断。
七、写一个“标准”宏,这个宏输入两个参数并返回较小的一个。
八、交换两个变量的值,不使用第三个变量。即a=3,b=5,交换之后a=5,b=3。
九、tcp连接建立的时候3次握手,断开连接的4次握手的具体过程。
十、epoll与select的区别。