0

I'm trying to get the current working directory in a unix system and stor it as a string to be used later. The code compiles but I get a segmentation fault when I try to run it. What am I doing wrong here?

#include<stdio.h>
#include<stdlib.h>

main() {
    FILE *p;
    char *ch;
    p = popen("pwd","r");
    sleep(1);
    if( p == NULL)
    {
        puts("Unable to open process");
    }
    fgets(ch,1024, p);
    printf("%s", ch);
    pclose(p);
}
kevin
  • 13
  • 1
  • 4

0 Answers0