3

我必须做我的功课,但我不能走 C 的道路。例如;

int main(void) {
  char *path;
  path = getenv("PATH");
  if(path)
    printf("The current path is: %s\n", path);
  return 0;
}

如果我运行这段代码,我会得到“分段错误(核心转储)”。我的操作系统是 Ubuntu 14.04 LTS。

4

1 回答 1

8

添加

#include <stdlib.h>

到列表顶部并尝试一下。它可能是 getenv 到 char * 指针的假定 int 返回,从而导致代码出错。

于 2014-12-07T21:35:35.930 回答