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
- //struct declaration
- struct integer {
- int* digits;
- int size;
- };
- //function declaration
- struct integer* read_integer(char* stringInt);
then i have a function to add the two large integers, and this is where i'm currently stuck
Expand|Select|Wrap|Line Numbers
- // 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;
- }
- }
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!