0

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;   
}
2
  • 1
    Function argument char *res is a copy of the variable passed, which remains unchanged. Commented May 12, 2017 at 15:40
  • Allergic to indentation? Commented May 12, 2017 at 15:55

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

i stoped increasing the pointer inside the addnums function. all it does is changing the copy of the pointer value to the calculated one and incremented the pointer in the addHugenNumbers if len1!=-1&&len2!=-1 but still it saves only the last calculated element
1

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.

7 Comments

yes i did that but it still isn't working. when i try to print the addednums array it has only one element, the last one that was calculated in the addHugeNums function
@ADR I edited my answer, please try now and let me know if you still have a problem.
int carry --> int carry = 0
it's an integer so the dafault value is 0
@ADR Actually, BLUEPIXY is right. I edited my answer. In C, integers that are not initialized have an undefined value.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.