So i was writting a simple c code which coverts a number into a string, and there is one thing that is unclear to me. On this code:
void itos(int a,char *p){
int pom = a;
do{
p++;
pom/=10;
}while(pom);
*p = '\0';
do{
pom = a%10 + '0';
*--p = pom;
a/=10;
}while(a);
}
i keep getting segmentation faults and i don't understand why. The string is passed by adress, so shouldn't the string from the main have the accses to string in the function? On the other hand this code works pefectly:
char *itos(int a,char *p){
int pom = a;
do{
p++;
pom/=10;
}while(pom);
*p = '\0';
do{
pom = a%10 + '0';
*--p = pom;
a/=10;
}while(a);
return p;
}
If somone could explain me the diffrence (especially why the first one isn't working) i'd be extremlly greatful.