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.