ADR
ADR

Reputation: 49

Adding numbers as strings in C

The problem is that even though I increase the pointer every time the addnums function is called in the end the addednums array contains only one character, the last one that was calculated. Why is this happening?

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>

/* run this program using the console pauser or add your own getch,system("pause") or input loop */

int addHugeNumbers(char *a1, char *a2,char *res) ;
int checkifnum(char *c1) ;
void addnums(char *a1, char *a2, char *res, int *ip) ;

int main(int argc, char *argv[]) {

char firstnum[255],secondnum[255],addednum[255] = {0};

/*Óôçí ðåñßðôùóç ðïõ ï ÷ñÞóôçò äþóåé ôÝôïéïõò áñéèìïýò þóôå íá ÷ñåéáóôåß êáé    256 bit ôüôå åìöáíßæåé
ôïí ìÝãéóôï áñéèìü ðïõ ìðïñåß ìå 255 bit*/

printf("Give the first number : "); scanf("%s",&firstnum);
printf("Give the second number : "); scanf("%s",&secondnum);
printf("%s %s\n", firstnum,secondnum) ;
printf("%d",addHugeNumbers(firstnum,secondnum,addednum)) ;
return 0;
}

int addHugeNumbers(char *a1, char *a2,char *res){
int carry,len1,len2,*ip,i;
ip = &carry ;
if ((checkifnum(a1) == 0)||(checkifnum(a2) == 0)) return 0;
len1 = strlen(a1) - 1;
len2 = strlen(a2) - 1;
a1 += strlen(a1) - 1;
a2 += strlen(a2) - 1;
//printf("%c %c\n",*a1,*a2) ;
do{
    addnums(a1,a2,res,ip) ;
    len1--;len2--;   
    if (len1!=-1 && len2!=-1) a1--,a2--;
}while(len1>-1 && len2>-1) ;
printf("%s\n",res) ;
return 1;
}

void addnums(char *a1, char *a2, char *res, int *ip){
*res++ = (char)((*a1 - '0' + *a2 - '0' + *ip) % 10 + '0');
*ip = (*a1 - '0' + *a2 - '0' + *ip) / 10;
}

int checkifnum(char *c1){
while (*c1) {
    if (isdigit(*c1++) == 0) return 0;
}
return 1;   
}

Upvotes: 0

Views: 2459

Answers (2)

RoaaGharra
RoaaGharra

Reputation: 710

The pointer that you are trying to increment is the local pointer inside addnums() which is a copy of the pointer in addHugeNums() which points to the same place each time.

Try to increment the pointer in addHugeNums()before you send a copy of it to addnums(). For example, you can try the following:

int addHugeNumbers(char *a1, char *a2,char *res){
int carry = 0;
int len1,len2,*ip;
ip = &carry ;
if ((checkifnum(a1) == 0)||(checkifnum(a2) == 0)) return 0;
len1 = strlen(a1) - 1;
len2 = strlen(a2) - 1;
a1 += strlen(a1) - 1;
a2 += strlen(a2) - 1;
//printf("%c %c\n",*a1,*a2) ;
char* tmp = res;
do{
    addnums(a1,a2,tmp,ip) ;
    tmp++;
    len1--;len2--;   
    if (len1!=-1 && len2!=-1) a1--,a2--;
}while(len1>-1 && len2>-1) ;
printf("%s\n",res) ;
return 1;
}

To further clarify, your code as is did the following during each iteration: It called the function addnums() and sent a copy of the pointer res as a paramater. Inside addnums() you increment the copy of res (leaving the original res unchanged) and then when you exit that function call that copy no longer exists.

On the next iteration you perform the exact same thing.

Upvotes: 1

Donnie
Donnie

Reputation: 46903

You are incrementing the local copy of the pointer, but not passing that back. You need res to be a char** and pass the address of the outer pointer for the increment to be visible outside your addnums function.

Upvotes: 2

Related Questions