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