【问题标题】:C Linux: warning: assignment makes integer from pointer without a cast [enabled by default]C Linux:警告:赋值从没有强制转换的指针生成整数 [默认启用]
【发布时间】:2014-12-03 17:26:25
【问题描述】:

我尝试在代码的不同位置强制转换 (char*),但没有任何帮助... 我也有以下错误:

OSCourseProject1.c: In function ‘main’:
OSCourseProject1.c:18:21: warning: assignment makes integer from pointer without a cast                       [enabled by default]
singleUserCommand = strtok(tempUserCommands," ");
                 ^
OSCourseProject1.c:19:29: warning: comparison between pointer and integer [enabled by     default]
while ( singleUserCommand != NULL )
                         ^
OSCourseProject1.c:21:4: warning: passing argument 1 of ‘strcmp’ makes pointer from integer without a cast [enabled by default]
if ( strcmp( singleUserCommand, "leave") != 0 )
^
In file included from OSCourseProject1.c:2:0:
/usr/include/string.h:144:12: note: expected ‘const char *’ but argument is of type ‘char’
extern int strcmp (const char *__s1, const char *__s2)
        ^
OSCourseProject1.c:25:5: warning: passing argument 2 of ‘strcmp’ makes pointer from     integer without a cast [enabled by default]
 while ( strcmp(tempShellPath, singleUserCommand)==0 &&
 ^
In file included from OSCourseProject1.c:2:0:
/usr/include/string.h:144:12: note: expected ‘const char *’ but argument is of type     ‘char’
extern int strcmp (const char *__s1, const char *__s2)
        ^
OSCourseProject1.c:33:8: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat=]
singleUserCommand);
    ^
OSCourseProject1.c:45:24: warning: assignment makes integer from pointer without a cast [enabled by default]
singleUserCommand = (char*)strtok(NULL, " ");
                    ^
/tmp/ccAuy2Eg.o: In function `main':
OSCourseProject1.c:(.text+0x11): undefined reference to `getnv'
collect2: error: ld returned 1 exit status

这是我写的代码(我应该从用户那里收到一行命令,然后 逐个命令分解它们并单独运行每个命令。

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

main ()
{
    char * shellPath = (char *)getnv("PATH");
    char * tempShellPath, singleUserCommand;
    char * singleShellCommand;
    char * userCommands = (char*)malloc(100);
    char * tempUserCommands = (char*)malloc(100);
    int forker, runOrDie = 1;
    while (runOrDie)
    {
        printf("<Please enter a command>");
        fgets(userCommands, 100, stdin);
        strcpy(tempUserCommands, userCommands);
        singleUserCommand = strtok(tempUserCommands," ");
        while ( singleUserCommand != NULL )
        {
            if ( strcmp( singleUserCommand, "leave") != 0 )
            {
                strcpy( (char*)tempShellPath, (char*)shellPath);
                singleShellCommand = (char*)strtok(tempShellPath, ":");
                while ( strcmp(tempShellPath, singleUserCommand)==0 &&
                    singleShellCommand != NULL )
                {
                    singleShellCommand = strtok(NULL, ":");
                }
                if ( singleShellCommand == NULL)
                {
                    printf("%s - no such command exists!", 
                            singleUserCommand);
                }
                else
                {
                    if ( (forker = fork()) == 0 )
                    {
                        execv(singleShellCommand, singleUserCommand);
                    }
                    else
                    {
                        wait( &forker );
                    }
                    singleUserCommand = (char*)strtok(NULL, " ");
                }
            }
            else
            {
                runOrDie = 0;
            }
        }
    }
} 

【问题讨论】:

  • getnv 错字 --> getenv

标签: c linux


【解决方案1】:
char * tempShellPath, singleUserCommand;

这将是

char *tempShellPath;
char singleUserCommand;

你需要的是:

char *singleUserCommand;

【讨论】:

    【解决方案2】:
    rather than trying to list all the problems via comments.
    (your original code yielded some 60 errors/warnings)
    the following code will compile with no errors/warnings
    Do read the notes in the file (most notes contain '<--'
    Do notice the error checking
    Do notice the new function cleanUp() to assure no memory leaks
    Do notice that second parameter in call to execv() still needs fixing
    
    always place literal on left when performing a comparison
         so compiler catches '=' .vs. '==' errors
    use meaningful names
    use proper return value typing and error checking
    
    // <-- added missing header files
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <unistd.h> // pid_t definition
    
    #include <sys/types.h>
    #include <sys/wait.h> // wait() prototype
    
    
    #define MAX_USER_COMMANDS_LEN (100) // <-- so changing size easy and no magic number in code
    
    // pointers here so visible to cleanUp and main
    char * shellPath          = NULL;
    char * tempShellPath      = NULL;
    char * userCommands       = NULL;
    char * tempUserCommands   = NULL;
    char * singleUserCommand  = NULL;
    char * singleShellCommand = NULL;
    
    // prototypes
    void cleanUp( void ); // <-- new function
    
    int main () // <-- corrected 
    {
        pid_t  childPid; // <-- corrected
        int    pidStatus; // status set by wait() // <-- corrected
    
        if( NULL == (shellPath = getenv("PATH")) ) // <-- added error checking
        { // then getenv failed
            perror( "getenv failed for PATH" );
            cleanUp();
            exit( EXIT_FAILURE );
        }
    
        // implied else, getenv successful
    
        if( NULL == (userCommands = malloc(MAX_USER_COMMANDS_LEN)) ) // <-- added error checking
        { // then, malloc failed
            perror( "malloc failed for userCommands" );
            cleanUp();
            exit( EXIT_FAILURE );
        }
    
        // implied else, malloc successful
    
        if( NULL == (tempShellPath = malloc( strlen(shellPath) +1) ) ) // added error checking
        { // then malloc failed
            perror( "malloc failed for tempShellPath" );
            cleanUp();
            exit( EXIT_FAILURE );
        }
    
        // implied else, malloc successful
    
        while (1)
        {
            printf("<Please enter a command>");
            if( NULL == fgets(userCommands, 100, stdin) ) // <-- added error checking
            { // then fgets failed
                perror( "fgets failed for userCommands" );
                cleanUp();
                exit( EXIT_FAILURE );
            }
    
            // implied else, fgets successful
    
            if( NULL == (tempUserCommands = malloc( strlen( userCommands )+1) ) ) // <-- added error checking
            { // then malloc failed
                perror( "malloc failed" );
                cleanUp();
                exit( EXIT_FAILURE );
            }
    
            // implied else, malloc successful
    
            strcpy(tempUserCommands, userCommands);
            singleUserCommand = strtok(tempUserCommands," ");
    
            while ( NULL != singleUserCommand )
            {
    
                // note: suggest: 'quit' or 'q' as intuitive rather than 'leave'
                if ( 0 == strcmp( singleUserCommand, "leave")  ) break; // exit while loop
    
                // +++++++++++++++++++++++++++++++++++++++++++++++++
                // replace following with something reasonable
                // perhaps a 'whereis' kind of call
                // note: shellPath is a series of directory paths, not executables separated by ':'
                //
                // strcpy( tempShellPath, shellPath);
                // singleShellCommand = (char*)strtok(tempShellPath, ":");
                // while (
                //     ( strcmp(tempShellPath, singleUserCommand)==0)
                //     &&
                //      (singleShellCommand != NULL) )
                // {
                //    singleShellCommand = strtok(NULL, ":");
                // }
                //
                // if ( singleShellCommand == NULL)
                // {
                //    printf(" - no command entered!\n");
                // }
                //
                // else
                // {
                    if ( 0 == (childPid = fork()) )
                    { // then child
                        // ERROR: singleUserCommand(in the following line)
                        //        must be an array of pointers to strings,
                        //        with a final pointer of NULL
                        if( -1 == execv(singleShellCommand, (char * const*)singleUserCommand) )
                        { // command failed // <-- added error checking
                            perror( "execv failed" );
                            cleanUp();  // test to assure this is 'ok' to use here
                            exit( EXIT_FAILURE ); // execv should never return
                        }
                    }
    
                    else
                    { // else parent
                        wait( &pidStatus ); // wait for only child to complete
                    } // end if
    
                    singleUserCommand = strtok(NULL, " ");
                // } // end if
            } // end while
            free( tempUserCommands ); tempUserCommands = NULL; // <-- prep for next pass through loop
        } // end while
    
        cleanUp(); // < -- added free of malloc'd areas
        return(0); // <-- added proper return statement
    } // end function: main
    
    // <-- added following function to assure no memory leaks
    void cleanUp()
    {
        free( tempShellPath ); tempShellPath = NULL;
        free( userCommands ); userCommands = NULL;
        free( tempUserCommands ); tempUserCommands = NULL;
    } // end function: cleanUp
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-07-04
      • 2015-10-19
      • 2013-09-14
      • 2016-06-21
      • 1970-01-01
      相关资源
      最近更新 更多