簡體   English   中英

C Linux:警告:賦值使指針從整數變成整數而沒有強制轉換[默認啟用]

[英]C Linux: warning: assignment makes integer from pointer without a cast [enabled by default]

我嘗試在代碼的不同位置強制轉換(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;
            }
        }
    }
} 
char * tempShellPath, singleUserCommand;

這將是

char *tempShellPath;
char singleUserCommand;

您需要的是:

char *singleUserCommand;
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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM