Search code examples
carraysunixgetcwd

Print getcwd path


My instructions are pretty clear, but I'm doing it wrong, can you help correct my error?

Instructions: else if (“pwd”) declare a char variable array of size MAX_PATH_LENGTH to hold the path do a getcwd print the path

my code:

 }else if(strcmp(argv[0],"pwd")){
        char arr[MAX_PATH_LENGTH];
        char getcwd(arr,MAX_PATH_LENGTH);
        printf("cwd: %s",arr); 

Solution

  • strcmp returns an integer and not a boolean.

    int strcmp (const char* str1, const char* str2);
    

    It returns a 0 if the two strings are equal, so you should be checking the returned value in your if statement like this:

    if(strcmp(argv[0],"pwd")==0)