# Thread: Help with this piece of code to multiply a string by two

1. ## Help with this piece of code to multiply a string by two

Below is a piece of code in a program I am making. This function takes in a char* and int argument, the char pointing to a string of numbers, and the int representing the length of the array.

When I run it like this, with the code commented out, and input 1234

Code:
```int multiplyByTwo(char *fraction, int fraction_length)
{
printf("last_Cell = %d",fraction_length);
printf("\n\nfraction[j] =" );
int j;
for(j=fraction_length;j>=0;j--){
fraction[j+1] = fraction[j];
}

fraction[0] = 0; printf("\n\n");
for(j=0;j<fraction_length+1;j++)
printf("%d ",fraction[j]);
}
/*
int i = fraction_length+1;
for(i; i >=1; i++){
fraction[i] *= 2;

if(fraction[i] >=10){
fraction[i] -= 10;
fraction[i-1] += 1;
}
}
int x;
for(x=0;x<=fraction_length;x++)
printf("%d ",fraction[x]);
}
*/```
the output is:

f[0] = 1 f[1] = 2 f[2] = 3 f[3] = 4 fractionlength = 4

0 1 2 3 4

When I include the commented out code, however, the output becomes:

f[0] = 1 f[1] = 2 f[2] = 3 f[3] = 4 fractionlength = 4
last_Cell = 4

Segmentation fault (core dumped)

What in the bejeezus is going on!?

EDIT: Sorry, the top line of output is actually coming from main, those are just tests that I ran on the parameters before calling the function.

2. Array indexing out of bounds, f[4] is undefined. Simply get rid of the "+1" on your last for loop.

3. In particular, why would the last printf statement, which printed the results of my moved array just fine sans the code, now stop working, even if there was something wrong with the code after it.

4. Wait, what? What loop are you referring to? The last loop before the comments works fine when the commented code is not included. Why would it stop working? Also, the whole point is to move each cell of the array up one block, and leave a 0 in the first cell. It looks like that's exactly what is happening when the code is commented out, but not so much when it is included.

5. You're accessing arrays out of bounds all over the place. Even the first loop:
Code:
```int j;
for(j=fraction_length;j>=0;j--){
fraction[j+1] = fraction[j];
}```
You start off by trying to access fraction[4], which is already one index past the array bounds. Then you try to access one past that with the "j + 1" statement. If fraction_length is equal to four, then you can only access 0-3. Not 0-5 like your code is doing.

Code:
```for(j=0;j<fraction_length+1;j++)
printf("%d ",fraction[j]);
}```
You're making the same mistake here, fortunately, it's easier to fix. Simply remove the "+1" and it will not overrun the bounds of the array.

Originally Posted by Avanish Giri
In particular, why would the last printf statement, which printed the results of my moved array just fine sans the code, now stop working, even if there was something wrong with the code after it.
...uh....what? I don't know what you had before, but it was probably because undefined behavior can sometimes seem to "work", or you hadn't tried to access it out of bounds before printing.

6. How could that be it though. I probably should have mentioned this, but I initialized the array to 50 cells. I'm merely trying to access the 4th or 5th or 6th one.

Just to be sure, I left the code in tact, and deleted the commented portion that I contend is causing some bizarre seg fault. Here is the relevant code. I know I'm probably missing some grave mistake here but if anyone could explain why this is happening I will be very much in your debt.

Code:
```#include <stdio.h>
#include <string.h>

int multiplyByTwo(char*, int);
int divideByTwo(char*, int);

int main(){

char array[256];
char fraction[50];
fgets(array,255,stdin);
int f;

int last_cell = strlen(array)-2;
int j;

for(j=0;j<last_cell;j++){
if(array[j] == '.')break;
}
int decimal_location = j;
int fraction_length = last_cell - decimal_location;
for(f=0;f<strlen(array)-1;f++){
array[f] -= 48;
}
int k=0;
for(j++;j<=last_cell;j++,k++){
fraction[k] = array[j];
printf("f[%d] = %d ",k,fraction[k]);
}
fraction[k+1] == 0;
fraction[k+2] == 0;
printf("fractionlength = %d\n",fraction_length - 1);
multiplyByTwo(fraction, fraction_length - 1);
printf("\ndecimal_location = %d\n",decimal_location);
printf("last_cell == %d\n",last_cell);
int num = 0;
int binary_array[50];
int s;
printf("Integral divided by two == ");
divideByTwo(array, decimal_location - 1);
}

int multiplyByTwo(char *fraction, int fraction_length)
{
printf("last_Cell = %d",fraction_length);
int j;
for(j=fraction_length;j>=0;j--){
fraction[j+1] = fraction[j];
}

fraction[0] = 0; printf("\n\n");
for(j=0;j<fraction_length+2;j++)
printf("%d ",fraction[j]);
}```

7. Grr...okay, I'll compile it.

Wait, no, I won't, because the linker complains at me. But looking at the warnings, you need to get a better compiler, or start listening to it, because you have unused variables, misused "==" symbols, and failure to return values at the ends of functions.

After deleting the functions that make the linker fail, I can't get it to segfault. What were you putting in?

8. Firstly, thank you so much for spending time helping me.

I added those == in a rush after reading your first reply, saying I was going out of bounds. I thought maybe I should initialize them to 0 since it could be holding a '/0'

It only Seg faults when I include the block of code in the FIRST post, the one that is Commented out in green. Something in there causes it to seg fault. Any ideas?

9. Well in the commented out loop i starts at fraction_length+1 and then goes up to... well probably something less than 0x7FFFFFFF seeing as how it'll crash well before it gets that high.

10. LOL!! That was it... but I'm crying on the inside. God how did I miss that, thanks!

11. Originally Posted by Avanish Giri