Adding large integers

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Shisou
    New Member
    • Jul 2007
    • 52

    Adding large integers

    hello everyone,

    Well I tried to solve this one on my own but it seems i need your help again :\

    I'm trying to write a program to add or subtract two large integers... easy enough..

    Where i run into problems is working with pointers. Here's where i'm at right now.

    I have a function that takes in the integers as strings and puts it into a struct

    Code:
    //struct declaration
    struct integer {
    	int* digits;
    	int size;
    };
    //function declaration
    struct integer* read_integer(char* stringInt);
    so far this works fairly well... except when i try to print the digits as integers it gives me the ascii equivilent number (like 54 for 1 or something)

    then i have a function to add the two large integers, and this is where i'm currently stuck

    Code:
    // function code
    struct integer* add(struct integer *p, struct integer *q){
           int *first, *second, carry, i, length;
           struct integer* final;
           carry = 0;
           if(p[0].size > q[0].size)
              length = p[0].size;
           else
              length = q[0].size;
           for(i=0;i<length;i++){
              if(p[0].digits[i] == "\0"){
                 //[WARNING] comparison between pointer and integer
                 first = 0;
              else
                 *first = p[0].digits[i];
              if(q[0].digits[i] == "\0")
                 //[WARNING] comparison between pointer and integer
                 second = 0;
              else
                 *second = q[0].digits[i];
              final[0].digits[i] = *first + *second + carry;
              if(final[0].digits[i] >= 10){
                 final[0].digits[i] = final[0].digits[i] - 10;
                 carry = 1;
              }
              else
                 carry = 0;
              }
              
    }
    compiling this gives me 2 warning on the lines above comments in code above.
    running it will give an access violation and crash the program.

    I'm not sure where i'm going wrong but i'm sure it has something to do with my use of pointers.

    Any help will be greatly appreciated!
    Last edited by Shisou; May 27 '08, 08:18 PM. Reason: wrong tags for code :(
  • Laharl
    Recognized Expert Contributor
    • Sep 2007
    • 849

    #2
    Using double quotes there means you're comparing it to a string literal. Use single quotes for the character literal you want.

    Comment

    • Shisou
      New Member
      • Jul 2007
      • 52

      #3
      ahhhh, that got rid of those pesky warnings :) thank you...

      however i still get an access violation, and i believe it is at the following line

      Code:
      *first = p[0].digits[i];
      atleast when i tried to step through this line in my debugger the error popped up :\

      Comment

      • oler1s
        Recognized Expert Contributor
        • Aug 2007
        • 671

        #4
        Code:
        int *first
        Why is first a pointer to an int? If you have described a valid reason for it to be a pointer, where does it point?

        Comment

        • Shisou
          New Member
          • Jul 2007
          • 52

          #5
          I didn't really have a reason for first and second to be pointers... except that it was the only way to get some kind of functionality out of the program... but now that i have the strings converted to integers I think i should be able to use just a regular int.

          I'm going to keep working this part alone and see where i can get, if I need anything else I'll be sure to post here again :)

          Thanks again!

          Comment

          Working...