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

标签 c linux

我尝试在代码的不同位置强制转换 (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;

关于C Linux : warning: assignment makes integer from pointer without a cast [enabled by default],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27277922/

相关文章:

c - 无效使用灵活数组 - 灵活结构数组作为另一个结构的成员

python - 使用 su 时 CTRL+C 未在 python 脚本中处理

c - 用 ALSA 录音

linux - 什么是基于内核的虚拟机?

c - 使用 ASN.1 编码和发送数据

C : scanf skips the first iteration while taking char* inputs from user through scanf

linux - Shell脚本中波浪号 "~/<folder>"与〜/"<folder>"有什么不同

linux - sort -R 命令不会在 Linux 中对行进行随机排序

c - C中Printf多个可变数量的字符串

c - 尝试返回 int 数组时返回错误 "address of stack memory associated with local variable ' processList'