getenv() segmentation fault

9.4k views Asked by At

I have to do my homework but I can not take path in C.For example ;

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

If I run this code , I got "Segmentation fault (core dumped)". My os is Ubuntu 14.04 LTS.

1

There are 1 answers

3
Brian Tiffin On BEST ANSWER

Add

#include <stdlib.h>

to the top of the listing and give that a try. It might be an assumed int return for getenv to char * pointer tripping up the code.