I tried to compile this program but I got a message saying "segmentation fault (core dumped)", by the compiler. Could anyone please tell me what is wrong?
#include <stdio.h>
#include <math.h>
#include <string.h>
#define power(x,y) (int)pow((double)x,(double)y)
/*-------------------setBits-------------------*/
/* A function to set the i (and following) bit(s) to be 'number'. The function takes 'number' and adds the matching powers of 2 to 'destination'. */
void setBits(int number, int i, int *destination) {
for( ; number!=0 ; number/=2 , i++)
(*destination) += (number % 2) * (power(2, i));
}
/*-------------------getDigit-------------------*/
/* A function that returns a string of 'number' converted to base 32. */
char getDigit(int number) {
char *digits = "0123456789ABCDEFGHIJKLMNOPQRSTUV";
return digits[number];
}
/*-------------------get32-------------------*/
/* A function that returns a string of 'number' converted to base 32. */
char *get32(int number) {
char *result = "";
if (number/32 == 0)
result[0] = getDigit(number);
else strcat(result,get32(number/32));
return result;
}
/*-------------------main-------------------*/
int main(){
int test = 0;
setBits(23, 5, &test);
printf("%s", get32(test));
return 0;
}
Also if anybody has tips to get the code better I'd love to get advices (: Thanks.