Hi Guys,
Sorry. I know this topic has been discussed millions of times. But I'm totally loss at the way C handles strings.
I do know in C, strings are arrays of chars. The last char must be a '\0' to indicate end of string. And erm...i guess that's all i know.
I've done a search but it's getting more confusing. Can anyone give me some sound advise, please?
I've a function "DecimaltoBinary" which takes in a integer parameter. You pass in a decimal, it will convert and return a value. It is supposed to return a string type which contains the binary representation of the decimal.
But in main(), how am I supposed to implement that?
I know it has something to do with pointers, but I'm like swimming in a vast ocean without any floatCan pointers or malloc() solev the problem?
Code:#include <stdio.h> char DecimaltoBinary(int i); int main(int argc, char *argv[]) { char s; int intDecimals=27; s= DecimaltoBinary(intDecimals); return 0; } char DecimaltoBinary(int i) { int NumOfChars=0,intTemp=1; char ChrArray[128]; char TempArray[128]; while(i!=1) { ChrArray[NumOfChars]=i%2; NumOfChars++; i/=2; } ChrArray[NumOfChars++]=1; TempArray[0]=1; while(intTemp<NumOfChars) TempArray[intTemp++]=ChrArray[NumOfChars-(intTemp+1)]; return TempArray; }



LinkBack URL
About LinkBacks
Can pointers or malloc() solev the problem?



I like these answers.
I wanted to indicate it as integer.... Erm...That means all my other lines are wrong too?