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;   
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
ADR
  • 49
  • 10

2 Answers2

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.

Donnie
  • 45,732
  • 10
  • 64
  • 86
  • 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 – ADR May 12 '17 at 15:54
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.

RoaaGharra
  • 710
  • 5
  • 19
  • 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 May 12 '17 at 15:57
  • @ADR I edited my answer, please try now and let me know if you still have a problem. – RoaaGharra May 12 '17 at 15:59
  • `int carry` --> `int carry = 0` – BLUEPIXY May 12 '17 at 16:13
  • it's an integer so the dafault value is 0 – ADR May 12 '17 at 16:23
  • 2
    @ADR Actually, BLUEPIXY is right. I edited my answer. In C, integers that are not initialized have an undefined value. – RoaaGharra May 12 '17 at 16:29
  • @BLUEPIXY Thanks. – RoaaGharra May 12 '17 at 16:33
  • @ADR If you want to further read on the issue, the first answer to [this question](http://stackoverflow.com/questions/1597405/what-happens-to-a-declared-uninitialized-variable-in-c-does-it-have-a-value) gives a clear explanation on uninitialized integer variables in C. – RoaaGharra May 12 '17 at 16:37