While循环受strcmp if语句的影响。为什么会这样?
基本上,在我的程序中,我对下面的while循环有一些问题。以下是我遇到问题的代码部分:While循环受strcmp if语句的影响。为什么会这样?,c,C,基本上,在我的程序中,我对下面的while循环有一些问题。以下是我遇到问题的代码部分: char *nameOfTheCommand; char *arrayArgs[500]; //track for redirection. If set, gives position of the file name. Else it equals zero int redirectionCheck=0;
char *nameOfTheCommand;
char *arrayArgs[500];
//track for redirection. If set, gives position of the file name. Else it equals zero
int redirectionCheck=0;
arrayArgs[0]=token;
int i;
i=0;
//While still arguements to take in, do this
while(arrayArgs[i]!=NULL)
{
i++;
arrayArgs[i]=strtok(NULL, " \n");
if(strcmp(arrayArgs[i], "<")==0)
{
redirectionCheck=i;
}
}
char*命令名;
char*arrayArgs[500];
//跟踪重定向。如果设置,则给出文件名的位置。否则等于零
int redirectionCheck=0;
arrayArgs[0]=令牌;
int i;
i=0;
//当仍有争论需要接受时,请这样做
while(arrayArgs[i]!=NULL)
{
i++;
arrayArgs[i]=strtok(空,“\n”);
如果(strcmp(arrayArgs[i],“在循环的最后一次迭代中,您将NULL
传递给strcmp
。这可以通过重新排列循环来避免:
i = 1;
// read subsequent tokens
while((arrayArgs[i] = strtok(NULL, " \n")) != NULL)
{
if(strcmp(arrayArgs[i], "<") == 0)
{
redirectionCheck = i;
}
i++;
}
i=1;
//读取后续标记
而((arrayArgs[i]=strtok(NULL,“\n”)!=NULL)
{
if(strcmp(arrayArgs[i],“在调试器中跟踪它?当strtok失败并将空指针传递给strcmp时会发生什么情况?”不应该if(strcmp(arrayArgs[i],“否,因为arrayArgs[0]预先设置为令牌。我正在设置arrayArgs[1]及更高版本,并有意跳过arrayArgs[0]@RetiredInja,我尝试过gdb,但诚实地遵循它时遇到了困难,尤其是在子进程方面。