首页 > 试题广场 >

下面程序的输出结果是( &n...

[单选题]
下面程序的输出结果是(      )
#include <stdio.h>
#include <stdlib.h>
void MallocMem(char* pc)
{
pc = (char*) malloc (100);

return;
}

int main()
{ 
char *str=NULL;

MallocMem(str);
strcpy(str,"hello ");
strcat(str+2, "world");

printf("%s",str);

return 0; 
}


  • hello world
  • 程序编译错误
  • 程序运行时崩溃
  • 其他几项都不对
char *str=NULL; 为空指针,调用函数MallocMem(str)在传参时传的是str的值,所以这就是一个值传递!值传递时是不会改变变量原本的值的,所以尽管给pc开辟了一个100字节大小的空间,但是pc与str实际是两个变量,所以str的值还为NULL,给NULL里面进行拷贝操作是错的。 使用指针一定要注意指针的操作范围和指向一块有效的内存!
编辑于 2019-09-01 16:45:29 回复(0)
缺少 #include <string> 导致运行崩溃;
发表于 2019-08-26 15:43:37 回复(1)
缺少头文件
发表于 2019-09-01 10:20:04 回复(0)
首先缺少string.h头文件,然后添加头文件后,编译成功运行后还是会段错误,在void MallocMem(char* pc)函数中,pc为形参,函数结束后被收回,所以str指向的依旧是NULL,显然对空指针是不能进行strcpy和strcat操作的。
发表于 2019-08-19 20:33:44 回复(0)
 [Error] 'strcpy' was not declared in this scope
 [Error] 'strcat' was not declared in this scope

缺少#include <string>
发表于 2019-08-17 16:37:01 回复(0)