-3
#include <stdio.h>
#include "funcs.h"

int main(void)
{
        /* varibles */
        float a[3];
        char operation;
        int num;

        /* header print */
        printf("Enter the operation you would like to use\n");
        scanf("%c", &operation);

        /* ifs */
        if(operation == "*") //warning is here
        {       
                printf("How many numbers would you like to use (max 4)\n");
                scanf("%d", &num);

                switch(num)
                {
                        case 2:
                                printf("enter your numbers\n");
                                scanf("%f", &a[0]);
                                scanf("%f", &a[1]);
                                printf(" the answer is %2f %2f", a[0] * a[1]);
                        break;
                }

        }
}

どうしたの?このエラーが発生します

Calculator.c: In function 'main':
Calculator.c:16:15: warning: comparison between pointer and integer [enabled by default]

なぜそれがコンパイルされないのか助けてください。今すぐ助けてください

4

2 に答える 2

5

変更してみる

operation == "*"

operation == '*'

文字列リテラル (二重引用符付きの "*") はconst char *(ポインター) でoperationあり、char(整数) であるため、これが問題になる可能性があります。あなたの警告があります。

これを修正するのは良いことです。これを無視すると、文字列へのポインターと文字を比較しているため、予想どおり 2 つの文字ではなく、非常に間違った動作 (ほぼ常に false) が発生します。

ps - @WhozCraig によって指摘された別のエラー (コンパイラではなくランタイムの可能性)、printf には 2 つの指定子( %2f) がありますが、変数は 1 つだけです。これにより、せいぜい未定義の動作が発生します。

次のように修正:

printf(" the answer is %2f", a[0] * a[1]);
于 2013-01-06T12:07:27.463 に答える
2

重引用符は文字用であり、二重引用符は文字列用であり、明らかに文字の同等性をチェックしているため、を変更する必要がoperation == "*"あります。operation == '*'

于 2013-01-06T12:09:43.503 に答える