1

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);
}
9
  • you are taking the address - not by reference. Commented Oct 18, 2014 at 18:51
  • 3
    myFraction.operator is a char, "+" is a const char[2] Commented Oct 18, 2014 at 18:51
  • 1
    use '+' instead of "+" Commented Oct 18, 2014 at 18:51
  • Nice it worked, thanks Machtl and thanks everyone else Commented Oct 18, 2014 at 18:53
  • As a side note case 4 does not require 'break;' Commented Oct 18, 2014 at 18:55

1 Answer 1

1

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

"+"

You want a character constant (ISO C99 6.4.4.4):

'+'
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.