When I pass in an array as a parameter in a function in C, does it create a copy of the array or does it actually make changes to the original array? I have a small program here that translates a number into its binary form. For example, if the number is 15, the binary form would be 1111.
void convertToBinary(int base10, int result, char *binaryResult){
int binaryVals[8] = {1,2,4,8,16,32,64,128};
if(base10 == 0){
printf("Binary Result %s", binaryResult);
}
else{
int max = 0;
for(int i = 0; i < 8; i++){
if(binaryVals[i] <= base10){
binaryResult[i] = '0';
}
else{
max = binaryVals[i-1];
binaryResult[i-1] = '1';
result = base10-max;
printf("Result %d", result);
break;
//convertToBinary(result,0, binaryResult);
}
}
}
}
int main(void){
char binaryResult[8];
convertToBinary(15,0,binaryResult);
}
The recursion part is failing me. I am not sure why. I suspect that it is because it is creating a copy of the array each time it runs the recursion. But I am not sure how to fix that. It would be great if someone could help, thanks