警告:与字符串比较结果不确定的行为 [英] Warning: comparison with string literals results in unspecified behaviour

查看:135
本文介绍了警告:与字符串比较结果不确定的行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始写在linux C.我一点都不精通C和使用Linux这正是我决定这将是一个好主意的原因简化外壳的一个项目。

与分析器开始,我已经遇到了一些问题。

在code应该是简单的,这就是为什么我不包括任何评论。

我正在与海湾合作委员会的警告:在用警告HERE评论行与不确定的行为字符串结果的比较(见下文code)。

我不知道为什么这会导致警告,但真正的问题是,即使我是一个比较<到一个&下;是里面的,如果...

没有得到

我要寻找的说明了问题的答案,但如果有你在code应改进请说出来看到的东西。只取一点,我并不精通,并且这仍是一项正在进行的工作(或更好,但在开始工作)。

先谢谢了。

 的#include<&stdio.h中GT;
#包括LT&;&unistd.h中GT;
#包括LT&;&string.h中GT;的typedef枚举{假的,真正的布尔};typedef结构{
    焦炭** ARG;
    字符* INFILE;
    字符* OUTFILE;
    INT背景;
} Command_Info;INT parse_cmd(字符* cmd_line,Command_Info * cmd_info)
{
    字符* ARG;
    字符* ARGS [100];    INT I = 0;
    ARG = strtok的(cmd_line,\\ n);
    而(ARG!= NULL){
        ARGS [I] = ARG;
        ARG =的strtok(NULL,\\ n);
        我++;
    }    诠释num_elems = I;    cmd_info-> INFILE = NULL;
    cmd_info-> OUTFILE = NULL;
    cmd_info->背景= 0;    INT iarg = 0;
    对于(i = 0; I< num_elems;我++)
    {
        如果(参数[I] ==&安培;)//警告HERE
            返回-1;
        否则,如果(参数[I] ==<)//警告HERE
            如果(参数[I + 1]!= NULL)
                cmd_info-> INFILE = ARGS [I + 1];
            其他
                返回-1;        否则,如果(参数[I] ==>中)//警告HERE
            如果(参数[I + 1]!= NULL)
                cmd_info-> OUTFILE = ARGS [I + 1];
            其他
                返回-1;        其他
            cmd_info-> ARG [iarg ++] = ARGS [I]
    }    cmd_info->精氨酸[iarg] = NULL;    返回0;
}无效print_cmd(Command_Info * cmd_info)
{
    INT I;
    对于(i = 0; cmd_info-> ARG [我] = NULL;!我++)
        的printf(ARG内容[%d] = \\%s \\的\\ n,我,cmd_info-> ARG [I]);
    的printf(ARG内容[%d] = \\%s \\的\\ n,我,cmd_info-> ARG [I]);
    的printf(INFILE = \\%s \\的\\ n,cmd_info-> INFILE);
    的printf(OUTFILE = \\%s \\的\\ n,cmd_info-> OUTFILE);
    的printf(后台= \\%d个\\\\ n,cmd_info->背景);
}INT主(INT ARGC,CHAR *的argv [])
{
    炭cmd_line [100];
    Command_Info cmd_info;    的printf(>>>中);    与fgets(cmd_line,100,标准输入);    parse_cmd(cmd_line,&安培; cmd_info);    print_cmd(安培; cmd_info);    返回0;
}


解决方案

您想要使用的strcmp()== 0 来比较字符串,而不是一个简单的 == ,这将只是比较,如果指针是相同的(他们不会在这种情况下)。

ARGS [I] 是一个指向字符串的指针(一个指向字符数组空值终止的),如&放大器; <

这位前pression ARGC [I] ==&放大器;检查,如果这两个指针是相同的(指向同一个内存位置)

这位前pression STRCMP(ARGC [I],与&)== 0 将检查两个字符串的内容是相同的。

I am starting a project of writing a simplified shell for linux in C. I am not at all proficient with C nor with Linux that's exactly the reason I decided it would be a good idea.

Starting with the parser, I have already encountered some problems.

The code should be straightforward that's why I didn't include any comments.

I am getting a warning with gcc: "comparison with string literals results in unspecified behaviour" at the lines commented with "WARNING HERE" (see code below).

I have no idea why this causes an warning, but the real problem is that even though I am comparing an "<" to an "<" is doesn't get inside the if...

I am looking for an answer for the problem explained, however if there's something that you see in the code that should be improved please say so. Just take in mind I am not that proficient and that this is still a work in progress (or better yet, a work in start).

Thanks in advance.

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

typedef enum {false, true} bool;

typedef struct {
    char **arg;
    char *infile;
    char *outfile;
    int background;
} Command_Info;

int parse_cmd(char *cmd_line, Command_Info *cmd_info)
{
    char *arg;
    char *args[100];    

    int i = 0;
    arg = strtok(cmd_line, " \n");
    while (arg != NULL) {
        args[i] = arg;
        arg = strtok(NULL, " \n");
        i++;
    }

    int num_elems = i;

    cmd_info->infile = NULL;
    cmd_info->outfile = NULL;
    cmd_info->background = 0;

    int iarg = 0;
    for (i = 0; i < num_elems; i++)
    {
        if (args[i] == "&") //WARNING HERE
            return -1;      
        else if (args[i] == "<") //WARNING HERE
            if (args[i+1] != NULL)
                cmd_info->infile = args[i+1];
            else
                return -1;

        else if (args[i] == ">") //WARNING HERE
            if (args[i+1] != NULL)
                cmd_info->outfile = args[i+1];
            else
                return -1;          

        else 
            cmd_info->arg[iarg++] = args[i];
    }

    cmd_info->arg[iarg] = NULL;

    return 0;   
}

void print_cmd(Command_Info *cmd_info)
{
    int i;  
    for (i = 0; cmd_info->arg[i] != NULL; i++)
        printf("arg[%d]=\"%s\"\n", i, cmd_info->arg[i]);
    printf("arg[%d]=\"%s\"\n", i, cmd_info->arg[i]);    
    printf("infile=\"%s\"\n", cmd_info->infile);
    printf("outfile=\"%s\"\n", cmd_info->outfile);
    printf("background=\"%d\"\n", cmd_info->background);
}

int main(int argc, char* argv[])
{
    char cmd_line[100];
    Command_Info cmd_info;

    printf(">>> ");

    fgets(cmd_line, 100, stdin);

    parse_cmd(cmd_line, &cmd_info);

    print_cmd(&cmd_info);

    return 0;
}

解决方案

You want to use strcmp() == 0 to compare strings instead of a simple ==, which will just compare if the pointers are the same (which they won't be in this case).

args[i] is a pointer to a string (a pointer to an array of chars null terminated), as is "&" or "<".

The expression argc[i] == "&" checks if the two pointers are the same (point to the same memory location).

The expression strcmp( argc[i], "&") == 0 will check if the contents of the two strings are the same.

这篇关于警告:与字符串比较结果不确定的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆