Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getenv() segmentation fault

Tags:

c

getenv

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.

like image 486
SemihY Avatar asked Oct 14 '25 18:10

SemihY


1 Answers

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.

like image 152
Brian Tiffin Avatar answered Oct 17 '25 09:10

Brian Tiffin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!