Function produces null string instead of output (C) -
Function produces null string instead of output (C) -
i writing programme can convert base of operations 10 number different base of operations between 2 , 16. have written function this, , think correct:
class="snippet-code-html lang-html prettyprint-override">char* baseconversion(int number, int base, char *word) { if (number != 0) { int x = number % base; if( x > 9){ if(x == 10) *word = 'a'; if(x == 11) *word = 'b'; if(x == 12) *word = 'c'; if(x == 13) *word = 'd'; if(x == 14) *word = 'e'; if(x == 15) *word = 'f'; baseconversion(number/base, base, word+1); } else { *word = x; baseconversion(number/base, base, word+1); } } }
i have main function set test it:
class="snippet-code-html lang-html prettyprint-override">int main(){ int num, base; char word[20]; scanf("%d %d", &num, &base); baseconversion(num, base, word); printf("%s", word); system("pause"); }
when feed input (my test case 15 16, should evaluate f) instead word null. not passing string correctly? or pointer arithmetic off?
note: aware give me reversed answer, can utilize prepare later when not getting null answer.
for num<10, aren't inserting character digit (for example, should set in '0' when x==0). verify: seek value letters in base of operations 16. (of course, never terminate string, either, you'll see funky stuff...)
(also, code won't work bases other 16, since have 16 hard-coded in there.)
c string pointers null
Comments
Post a Comment