首页 > 试题广场 >

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

[单选题]
下面程序的输出结果是()
#include <stdio.h>
#include <stdlib.h>
#include <string.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
  • 程序编译错误
  • 程序运行时崩溃
  • 其他几项都不对
linux@ubuntu:~$ gcc test.c -o l
test.c: In function ‘main’:
test.c:15:2: warning: incompatible implicit declaration of built-in function ‘strcpy’ [enabled by default]
test.c:16:2: warning: incompatible implicit declaration of built-in function ‘strcat’ [enabled by default]
linux@ubuntu:~$ ./l
Segmentation fault (core dumped)

发表于 2019-10-24 11:04:42 回复(0)

出题不严谨.

目的是考察str当实参传入有没有变化。

应该包含#include

发表于 2019-10-19 09:37:44 回复(0)
作为形参的pc和作为实参的str不是同一个
在调用的时候是值传递,那里的pc在返回后就被销毁了
*str还是0
顺便,这道题按理说编译都过不去(没有string.h)

发表于 2019-09-20 15:17:00 回复(0)
程序运行之后显示的是段错误
发表于 2019-08-23 19:20:56 回复(1)