Why does the compiler issue “warning: assignment m

2019-07-25 11:27发布

问题:

I don't understand why I keep getting an error when I try to compile it. I get an error saying

fractions.c: In function "main":
fractions.c:35:24: warning: assignment makes integer from pointer without a cast [enabled by default] fractions.c:38:24: warning: assignment makes integer from pointer without a cast [enabled by default] fractions.c:41:24: warning: assignment makes integer from pointer without a cast [enabled by default] fractions.c:44:24: warning: assignment makes integer from pointer without a cast [enabled by default] fractions.c:48:11: warning: unused variable "fArray" [-Wunused-variable]"

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

typedef struct{
        int numerator;
        int denomonator;
        char operator;
        int num2;
        int den2;
} fraction;

void printFraction(fraction f);

int main(){
        fraction myFraction;
        char input;

        printf("Enter the numerator of fraction 1: ");
        scanf("%i", &myFraction.numerator);
        printf("Enter the demoninator of fraction 1: ");
        scanf("%i", &myFraction.denomonator);

        printf("Enter the numerator of fraction 2: ");
        scanf("%i", &myFraction.num2);
        printf("Enter the demoninator of fraction 2: ");
        scanf("%i", &myFraction.den2);

        printf("\n\nEnter the operation you would like to perform\n");
        printf("(1) for addition\n(2) for subtraction\n(3) for multiplication)");
        printf("\n(4) for division\n");
        scanf("%c", &input);

        switch(input){
                case '1':
                        myFraction.operator = "+";
                        break;
                case '2':
                        myFraction.operator = "-";
                        break;
                case '3':
                        myFraction.operator = "*";
                        break;
                case '4':
                        myFraction.operator = "/";
                        break;
        }

        fraction fArray[] = {myFraction};
        printFraction(myFraction);

        return 0;
}

void printFraction(fraction f){
        printf("%i/%i %c %i/%i\n", f.numerator, f.denomonator, f.operator, f.num2, f.den2);
}

回答1:

This here is a string literal (ISO C99 6.4.5):

"+"

You want a character constant (ISO C99 6.4.4.4):

'+'