论坛: 编程破解 标题: 笔试遇到的几道C语言题,放上来大家做做 复制本贴地址    
作者: sniper167 [sniper167]    论坛用户   登录
昨天笔试,做了几道C语言的题,放上来大家做做  一共四道
网上有答案,请大家别直接贴上来,没意思,自己做做,大家来讨论讨论
------------------------
void GetMemory(char *p)
{
p = (char *)malloc(100);
}
void Test(void)
{
char *str = NULL;
GetMemory(str);
strcpy(str, "hello world");
printf(str);
}

请问运行Test函数会有什么样的结果?


char *GetMemory(void)
{
char p[] = "hello world";
return p;
}
void Test(void)
{
char *str = NULL;
str = GetMemory();
printf(str);
}

请问运行Test函数会有什么样的结果?



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函数会有什么样的结果?



void Test(void)
{
char *str = (char *) malloc(100);
strcpy(str, “hello”);
free(str);
if(str != NULL)
{
strcpy(str, “world”);
printf(str);
}
}
请问运行Test函数会有什么样的结果?


地主 发表时间: 05-12-01 20:41

回复: ziaichen [ziaichen]   论坛用户   登录
程序一:编译没错,但没有任何结果显示,即为空值,程序有内在的错误
程序二:输出"hello",若将char p[] = "hello world";改为char *p = "hello world";则输出hello world
程序三:输出"hello"
程序四:输出"world"

这个题目主要是对字符串,字符数组,指针,空值及分配空间的考查.


[此贴被 一窍不通(ziaichen) 在 12月08日13时04分 编辑过]

B1层 发表时间: 05-12-08 12:26

论坛: 编程破解

20CN网络安全小组版权所有
Copyright © 2000-2010 20CN Security Group. All Rights Reserved.
论坛程序编写:NetDemon

粤ICP备05087286号