Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ssl/3.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 - Fatal编程技术网

C 编译器警告我的函数永远不会执行

C 编译器警告我的函数永远不会执行,c,C,我定义了一个名为checkrow的函数,它读取字符数组的给定元素(通过指向char的指针提供给它),并确定它是否等于某些字符。当我编译整个程序时,编译器告诉我第67行,即(if(*pInput==(“”| |'\t'| |'\n'))将永远不会执行。为什么?这个逻辑对我来说是有道理的。下面是代码 #include <stdio.h> #include <stdlib.h> int checkRow(char *pArray); int main (void){

我定义了一个名为checkrow的函数,它读取字符数组的给定元素(通过指向char的指针提供给它),并确定它是否等于某些字符。当我编译整个程序时,编译器告诉我第67行,即
(if(*pInput==(“”| |'\t'| |'\n'))
将永远不会执行。为什么?这个逻辑对我来说是有道理的。下面是代码

#include <stdio.h>
#include <stdlib.h>

int checkRow(char *pArray);

int main (void){

    char c = 0;
    int high = 0;
    int low = 9;
    int checksum = 0;
    char input[3000];
    char *pInput = input;
    FILE *pFile = NULL;

    pFile = fopen("foo.txt", "r");
    if (pFile == NULL){
        printf("failed to open file\n");
        return (-1);
    }

    while((c = fgetc(pFile)) != EOF){
        *pInput = c;
        ++pInput;
    }

    fclose(pFile);
    pFile = NULL;
    pInput = input; //point pInput back to the address of the first element of input
    //printf("the checksum is %d\n", checksum(pInput));
    while(*pInput){

        if(checkRow(pInput) == 0){
            checksum += (high - low);
            ++pInput;
            continue;
        }
        else{
            if((*pInput - '0') > high && (*pInput - '0') < low){
            high = *pInput - '0';
            low = *pInput - '0';
            ++pInput;
            continue;
            }
            else if (*pInput - '0' > high){
            high = *pInput - '0';
            ++pInput;
            continue;
            }
            else if(*pInput - '0' < low){
            low = *pInput - '0';
            ++pInput;
            }
        }

    }

    printf("this is the checksum %d\n", checksum);
    getchar();
    return 0;
}

int checkRow(char *pInput){

    int in = 0;

    if(*pInput == (' ' || '\t' || '\n'))
        in = 0;
    else
        in = 1;
    return in;
}
#包括
#包括
int checkRow(字符*pArray);
内部主(空){
字符c=0;
int高=0;
int低=9;
整数校验和=0;
字符输入[3000];
char*pInput=输入;
FILE*pFile=NULL;
pFile=fopen(“foo.txt”,“r”);
if(pFile==NULL){
printf(“打开文件失败\n”);
返回(-1);
}
而((c=fgetc(pFile))!=EOF){
*pInput=c;
++pInput;
}
fclose(pFile);
pFile=NULL;
pInput=input;//将pInput指向输入的第一个元素的地址
//printf(“校验和为%d\n”,校验和(pInput));
while(*pInput){
如果(检查行(pInput)==0){
校验和+=(高-低);
++pInput;
持续
}
否则{
如果((*pInput-'0')>高和(*pInput-'0')<低){
高=*引脚输出-'0';
低=*引脚输出-'0';
++pInput;
持续
}
否则,如果(*pInput-'0'>高){
高=*引脚输出-'0';
++pInput;
持续
}
否则,如果(*输入-'0'<低){
低=*引脚输出-'0';
++pInput;
}
}
}
printf(“这是校验和%d\n”,校验和);
getchar();
返回0;
}
int checkRow(字符*pInput){
int in=0;
if(*pInput==(“”| |’\t’| |’\n'))
in=0;
其他的
in=1;
返回;
}

并不是说该语句不会被执行,只是其中的特定表达式。警告消息如下所示:

testcompare.c:67:35: warning: code will never be executed [-Wunreachable-code]
    if(*pInput == (' ' || '\t' || '\n'))
                                  ^~~~
testcompare.c:67:27: warning: code will never be executed [-Wunreachable-code]
    if(*pInput == (' ' || '\t' || '\n'))
                          ^~~~
请注意指向
'\n'
'\t'
的箭头,它们所说的永远不会执行


|
运算符是一个短路运算符,它仅在左操作数为假时执行右操作数


在这种情况下,由于
'
是一个常量,并且已知它是truthy(除了
'\0'
之外的任何
char
都是truthy),因此不需要执行其他两个操作数来确定结果。

这并不是说语句不会被执行,只是其中的特定表达式。警告消息如下所示:

testcompare.c:67:35: warning: code will never be executed [-Wunreachable-code]
    if(*pInput == (' ' || '\t' || '\n'))
                                  ^~~~
testcompare.c:67:27: warning: code will never be executed [-Wunreachable-code]
    if(*pInput == (' ' || '\t' || '\n'))
                          ^~~~
请注意指向
'\n'
'\t'
的箭头,它们所说的永远不会执行


|
运算符是一个短路运算符,它仅在左操作数为假时执行右操作数


在这种情况下,由于
'
是一个常量,并且已知它是truthy(除
'\0'
之外的任何
char
都是truthy),因此不需要执行其他两个操作数来确定结果。

您需要重写表达式:

if(*pInput == (' ' || '\t' || '\n'))
作为


您需要重写表达式:

if(*pInput == (' ' || '\t' || '\n'))
作为


只有当第一个操作数的计算结果为0时,
|
运算符才计算其第二个操作数

因此,在
“||”\t“||”\n”
中,不会计算最后一个操作数,因为
“||”\t”
为非零

另一方面,你想写一些

(*pInput == ' ') || (*pInput == '\t') || (*pInput == '\n')
您犯的另一个错误是,您忘记在字符串
input

while((c = fgetc(pFile)) != EOF){
    *pInput = c;
    ++pInput;
}

fclose(pFile);
pFile = NULL;
*pInput = 0;       /* THIS IS IMPORTANT */
pInput = input;  

while(*pInput)

只有当第一个操作数的计算结果为0时,
|
运算符才计算其第二个操作数

因此,在
“||”\t“||”\n”
中,不会计算最后一个操作数,因为
“||”\t”
为非零

另一方面,你想写一些

(*pInput == ' ') || (*pInput == '\t') || (*pInput == '\n')
您犯的另一个错误是,您忘记在字符串
input

while((c = fgetc(pFile)) != EOF){
    *pInput = c;
    ++pInput;
}

fclose(pFile);
pFile = NULL;
*pInput = 0;       /* THIS IS IMPORTANT */
pInput = input;  

while(*pInput)

*pInput==(“| |”\t“| |”\n”)
根本不符合您的想法。您应该查找这些运算符的作用。
|
是在逻辑表达式之间进行的,而不是在要比较的常量之间。@EugeneSh True,但为什么这一行永远不会执行?编译器知道指针永远不能(可能)为1?这将阻止下一行的执行…正确的方法是
如果((*pInput='')|(*pInput='\t')|(*pInput='\t')){..}
@Yunnosch我认为OP以某种方式扭曲了最初的警告。
*pInput==('\t'\t'\124;'\ n')
根本不符合您的想法。您应该查找这些运算符的作用。
|
是在逻辑表达式之间进行的,而不是在要比较的常量之间。@EugeneSh True,但为什么这一行永远不会执行?编译器知道指针永远不能(可能)为1?这将阻止下一行的执行…正确的方法是
如果((*pInput=''''\t')|(*pInput=''\t')|(*pInput='\'')){..}
@Yunnosch我认为OP以某种方式扭曲了最初的警告。