Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 这是未定义的吗?_C_Undefined Behavior - Fatal编程技术网

C 这是未定义的吗?

C 这是未定义的吗?,c,undefined-behavior,C,Undefined Behavior,我的编译器警告:对j的操作可能未定义 以下是C代码: for(int j = 1; pattern[j] != '\0' && string[i] != '\0';){ if(string[i+j] != pattern[j++]){//this is on the warning found = 0; break; } } 这是未定义的吗?是的字符串[i+j]!=模式[j++]基于变量j有两种不同的执行,中间没有任何变量。所以这

我的编译器警告:
对j的操作可能未定义

以下是C代码:

for(int j = 1; pattern[j] != '\0' && string[i] != '\0';){
    if(string[i+j] != pattern[j++]){//this is on the warning
        found = 0;
        break;
    }
}

这是未定义的吗?

是的<代码>字符串[i+j]!=模式[j++]基于变量
j
有两种不同的执行,中间没有任何变量。所以这是一个例子。

是的。C11标准在§6.5中规定:

If a side effect on a scalar object is unsequenced relative to either a different 
side effect on the same scalar object or a value computation using the value of the 
same scalar object, the behavior is undefined. If there are multiple allowable 
orderings of the subexpressions of an expression, the behavior is undefined if such 
an unsequenced side effect occurs in any of the orderings.
这里,在比较中

if(string[i+j] != pattern[j++])

您正在使用
模式[j++]
增加
j
的值,并在
字符串[i+j]
中使用
j
的值。
j++
的副作用相对于值计算
i+j
没有排序。这是典型的未定义行为

“i”在哪里声明/初始化?@MartinJames这无关紧要。您可能会发现一篇有趣的文章。我认为问题与@2013Asker有关,您使用了哪个编译器?您请求的编译器现在可用:-)