469,603 Members | 2,057 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,603 developers. It's quick & easy.

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

Expand|Select|Wrap|Line Numbers
  1. //struct declaration
  2. struct integer {
  3.     int* digits;
  4.     int size;
  5. };
  6. //function declaration
  7. 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

Expand|Select|Wrap|Line Numbers
  1. // function code
  2. struct integer* add(struct integer *p, struct integer *q){
  3.        int *first, *second, carry, i, length;
  4.        struct integer* final;
  5.        carry = 0;
  6.        if(p[0].size > q[0].size)
  7.           length = p[0].size;
  8.        else
  9.           length = q[0].size;
  10.        for(i=0;i<length;i++){
  11.           if(p[0].digits[i] == "\0"){
  12.              //[WARNING] comparison between pointer and integer
  13.              first = 0;
  14.           else
  15.              *first = p[0].digits[i];
  16.           if(q[0].digits[i] == "\0")
  17.              //[WARNING] comparison between pointer and integer
  18.              second = 0;
  19.           else
  20.              *second = q[0].digits[i];
  21.           final[0].digits[i] = *first + *second + carry;
  22.           if(final[0].digits[i] >= 10){
  23.              final[0].digits[i] = final[0].digits[i] - 10;
  24.              carry = 1;
  25.           }
  26.           else
  27.              carry = 0;
  28.           }
  30. }
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!
May 27 '08 #1
4 5702
849 Expert 512MB
Using double quotes there means you're comparing it to a string literal. Use single quotes for the character literal you want.
May 27 '08 #2
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

Expand|Select|Wrap|Line Numbers
  1. *first = p[0].digits[i];
atleast when i tried to step through this line in my debugger the error popped up :\
May 27 '08 #3
671 Expert 512MB
Expand|Select|Wrap|Line Numbers
  1. 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?
May 28 '08 #4
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!
May 28 '08 #5

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

4 posts views Thread by DJTB | last post: by
6 posts views Thread by Michael Zielinski | last post: by
5 posts views Thread by Edith Gross | last post: by
4 posts views Thread by KC | last post: by
17 posts views Thread by Sri | last post: by
42 posts views Thread by yong | last post: by
reply views Thread by guiromero | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.