Like Tree4Likes
  • 1 Post By stahta01
  • 2 Post By stahta01
  • 1 Post By iMalc

getting a "invalid operands to binary"

This is a discussion on getting a "invalid operands to binary" within the C Programming forums, part of the General Programming Boards category; I am getting this error in lines that involve "ch[_]" in lines 27, 28, 29, 33, 42, 43, 44, and ...

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    25

    getting a "invalid operands to binary"

    I am getting this error in lines that involve "ch[_]" in lines 27, 28, 29, 33, 42, 43, 44, and 48, heres the code:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #include <string.h>
    
    
    struct integer{
        int* digits;
        int size;
    };
    FILE *IFP;
    //convert string to int array
    
    
    
    
    struct integer* multiply(struct integer *p,  struct integer *q){
        struct integer *done = malloc(sizeof(struct integer));
        struct integer *temp = malloc(sizeof(int) * 1000000000);
        struct integer *ch = malloc(sizeof(char) * 1000000000);
        done->digits = malloc(sizeof(int) * (done->size + 1));
        int i = 0, j = 0, k = 0, d = 0, l1, l2;
        long int carry = 0;
    
    
        l1 = p->size - 1;
        l2 = q->size - 1;
        if (l1 >= l2){
            for (i = l2; i >= 0; i--){
                for (j = l1; j >= 0; j--){
                    ch[k] += (q->digits[i] * p->digits[j] + carry) % 10;
                    temp[k] = ch[k] % 10;
                    temp[k+1] = ch[k] / 10;
                    k++;
                    carry = (q->digits[i] * p->digits[j] + carry) / 10;
                }
                temp[k++] += carry;
                carry = 0;
                ch = temp;
                d++;
                k = d;
            }
        }else{
            for (i = l1; i >= 0; i--){
                for (j = l2; j >= 0; j--){
                    ch[k] += (q->digits[j] * p->digits[i] + carry) % 10;
                    temp[k] = ch[k] % 10;
                    temp[k+1] = ch[k] / 10;
                    k++;
                    carry = (q->digits[j] * p->digits[i] + carry) / 10;
                }
                temp[k++] += carry;
                carry = 0;
                ch = temp;
                d++;
                k = d;
            }
        }
        done->digits = temp;
        return done;
    }
    
    
    struct integer* convert_integer(char* stringInt){
        struct integer *converted = malloc(sizeof(struct integer));
        int length, i, *ints;
        ints = (int *)malloc(10001 * sizeof(int));
        length = strlen(stringInt);
        converted->size = length;
        for(i = length; i > 0; i--)
            ints[i - 1] = (stringInt[length - i]-48);
        converted->digits = ints;
        return converted;
    }
    
    
    void print(struct integer *a){
        int len = a->size;
        int i, j;
        for(i = 1000000000; i >= 0; i--){
            if (a->digits[i] != 0){
                for (j = i; j >= 0; j--){
                    printf("%d", a[j]);
                }
                break;
            }
            printf("\n");
        }
    }
    
    
    int main(){
        IFP = fopen("bigint.txt", "r");
        char *num1, *num2;
        num1 = (char *)malloc(10001 * sizeof(char));
        num2 = (char *)malloc(10001 * sizeof(char));
        struct integer *snum1 = malloc(sizeof(struct integer));
        struct integer *snum2 = malloc(sizeof(struct integer));
        struct integer *total = malloc(sizeof(struct integer));
        int i, j, runs = 0;
        fscanf(IFP, "%d", &runs);
        printf("runs: %d\n", runs);
        for (i = 0; i < runs; i++){
            fscanf(IFP, "%s %s", num1, num2);
            printf("Problem #%d: ", runs+1);
            snum1 = convert_integer(num1);
            print(snum1);
            printf(" * ");
            snum2 = convert_integer(num2);
            print(snum2);
            printf(" = ");
            total = multiply(snum1, snum2);
            //print(total);
        }
        free(snum1->digits);
        free(snum1);
        free(snum2->digits);
        free(snum2);
        fclose (IFP);
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Jan 2013
    Posts
    25
    the lines appear to be off, sorry for this

  3. #3
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    520
    Code:
    t.c:31:23: error: invalid operands to binary + (have ‘struct integer’ and ‘long int’)
    t.c:32:33: error: invalid operands to binary % (have ‘struct integer’ and ‘int’)
    t.c:33:35: error: invalid operands to binary / (have ‘struct integer’ and ‘int’)
    t.c:37:23: error: invalid operands to binary + (have ‘struct integer’ and ‘long int’)
    t.c:46:23: error: invalid operands to binary + (have ‘struct integer’ and ‘long int’)
    t.c:47:33: error: invalid operands to binary % (have ‘struct integer’ and ‘int’)
    t.c:48:35: error: invalid operands to binary / (have ‘struct integer’ and ‘int’)
    What the compiler is telling you is "I don't know how to <operator> a struct integer and an integer". Where <operator> is + & or / etc.. shown above. Meaning you can't perform mathematical operations on a "struct integer", only on integer/floating point types. So you have to reference a member INSIDE the "struct integer" instead and use THAT in the operator (as an operand). If you read the errors its actually quite clear the problem.

    EDIT:
    If it's not obvious "binary" in this case means "takes 2 arguments" ie, + takes 2 "operands" a + b, a and b are operands.

  4. #4
    Registered User
    Join Date
    Jan 2013
    Posts
    25
    okay i fixed it and now it is just crashing, any reason known?
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #include <string.h>
    
    
    struct integer{
        int* digits;
        int size;
    };
    FILE *IFP;
    struct integer* multiply(struct integer *p,  struct integer *q){
        struct integer *done = malloc(sizeof(struct integer));
        int *temp = malloc(sizeof(int) * 1000000000);
        int *ch = malloc(sizeof(char) * 1000000000);
        done->digits = malloc(sizeof(int) * (done->size + 1));
        int i = 0, j = 0, k = 0, d = 0, l1, l2;
        long int carry = 0;
        l1 = p->size - 1;
        l2 = q->size - 1;
        if (l1 >= l2){
            for (i = l2; i >= 0; i--){
                for (j = l1; j >= 0; j--){
                    ch[k] += (q->digits[i] * p->digits[j] + carry) % 10;
                    temp[k] = ch[k] % 10;
                    temp[k+1] = ch[k] / 10;
                    k++;
                    carry = (q->digits[i] * p->digits[j] + carry) / 10;
                }
                temp[k++] += carry;
                carry = 0;
                ch = temp;
                d++;
                k = d;
            }
        }else{
            for (i = l1; i >= 0; i--){
                for (j = l2; j >= 0; j--){
                    ch[k] += (q->digits[j] * p->digits[i] + carry) % 10;
                    temp[k] = ch[k] % 10;
                    temp[k+1] = ch[k] / 10;
                    k++;
                    carry = (q->digits[j] * p->digits[i] + carry) / 10;
                }
                temp[k++] += carry;
                carry = 0;
                ch = temp;
                d++;
                k = d;
            }
        }
        for(i = 1000000000; i >= 0; i--){
            if (temp[i] != 0){
                for (j = i; j >= 0; j--){
                    printf("%d", temp[j]);
                }
                break;
            }
            printf("\n");
        }
        done->digits = temp;
        return done;
    }
    
    
    struct integer* convert_integer(char* stringInt){
        struct integer *converted = malloc(sizeof(struct integer));
        int length, i, *ints;
        ints = (int *)malloc(10001 * sizeof(int));
        length = strlen(stringInt);
        converted->size = length;
        for(i = length; i > 0; i--)
            ints[i - 1] = (stringInt[length - i]-48);
        converted->digits = ints;
        return converted;
    }
    
    
    int main(){
        IFP = fopen("bigint.txt", "r");
        char *num1, *num2;
        num1 = (char *)malloc(10001 * sizeof(char));
        num2 = (char *)malloc(10001 * sizeof(char));
        struct integer *snum1 = malloc(sizeof(struct integer));
        struct integer *snum2 = malloc(sizeof(struct integer));
        struct integer *total = malloc(sizeof(struct integer));
        int i, j, runs = 0;
        fscanf(IFP, "%d", &runs);
        for (i = 0; i < runs; i++){
            fscanf(IFP, "%s %s", num1, num2);
            printf("Problem #%d: ", i + 1);
            snum1 = convert_integer(num1);
            for(j = 0; j < snum1->size; j++)
                printf("%d",snum1->digits[snum1->size - j - 1]);
            printf(" * ");
            snum2 = convert_integer(num2);
            for(j = 0; j < snum2->size; j++)
                printf("%d",snum2->digits[snum2->size - j - 1]);
            printf(" = ");
            total = multiply(snum1, snum2);
        }
        free(snum1->digits);
        free(snum1);
        free(snum2->digits);
        free(snum2);
        fclose (IFP);
        return 0;
    }

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    2,770
    Code:
    void print(struct integer *a){
        int len = a->size;
        int i, j;
        for(i = 1000000000; i >= 0; i--){ /* Mistake in this Line */
            if (a->digits[i] != 0){       /* Off by one  mistake in this line */
                for (j = i; j >= 0; j--){ /* or in this line */
                    printf("%d", a[j]);   /* Mistake in this Line */
                }
                break;
            }
            printf("\n");
        }
    }
    Tim S.
    Salem likes this.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    2,770
    You are allocating enough space that it might be crashing because it fails.

    int *temp = malloc(sizeof(int) * 1000000000); /* I estimate this as 4000 MB the normal safe amount is around 10 MB */

    Code:
        int *temp = malloc(sizeof(int) * 1000000000);
        if (temp == NULL){
            fprintf(stderr, "malloc of temp failed\n");
            exit(1);
        }
    When I added the above it it printed the failed message.


    Tim S.
    Last edited by stahta01; 01-25-2013 at 10:15 PM.
    nonpuz and Salem like this.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Your program attempts to allocate 5GB, plus an uninitialised amount (done->size) which could easily also be up to another 4GB.
    A 32-bit process in Windows, for example, can only allocate 2GB in total, and usually not all at once.
    You need to be a lot starter about your allocation requests.
    Clayton Cuteri likes this.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  8. #8
    Registered User
    Join Date
    May 2009
    Posts
    2,770
    FYI: I posted test code for someone else doing what appears to be the same assignment in this thread.
    Can someone try to explain what my professor is talking about...
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. No operator matches "[]" these operands And error C2248
    By VolcanoWolcano in forum C++ Programming
    Replies: 2
    Last Post: 06-07-2012, 07:56 AM
  2. Replies: 6
    Last Post: 08-17-2009, 10:43 AM
  3. invalid operands to binary %
    By Mathsniper in forum C Programming
    Replies: 3
    Last Post: 12-03-2005, 11:21 PM
  4. An error "invalid combination of operands and opcodes"!!
    By praseodeveloper in forum C++ Programming
    Replies: 4
    Last Post: 10-20-2005, 02:00 AM
  5. invalid operands to binary ^ ?
    By seal in forum C Programming
    Replies: 14
    Last Post: 09-13-2005, 05:59 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21