I am trying to convert an int to a string using a function instead of writing sprintf each time.
For some reason i'm getting "Segmentation fault" error.
I have been trying to fix it for a while now
(and yes, i am beginner at c)
#include <stdio.h>
#include <iostream>
char* tostring(int n){
char* output;
sprintf(output,"%d",n);
return output;
}
void writeTable(int tb[3][3]){
printf("\n");
for (int x=0;x<3;x++){
char tmp[3];
for(int y=0;y<3;y++){
if (tb[x][y] == 1){tmp[y] = char(88); continue;}
if (tb[x][y] == 2){tmp[y] = char(79); continue;}
tmp[y] = char(32);
};
printf(" %c | %c | %c\n",tmp[0],tmp[1],tmp[2]);
if (x!=2){
printf(" ━━━━━━━━━\n");
};
};
}
int main(){
bool isPlayer2Turn = false;
int table[3][3] = {
{2, 2, 2},
{0, 1, 0},
{0, 0, 0},
};
std::system("cls");
writeTable(table);
printf("%s",tostring(69));
return 0;
}```