0

when I call this function in main, I need to convert the 'longlong num' into string and RETURN the string...

so in main

main()
{
    char string[20];
    
    ........
    string = LongLongToString(random);
}


char LongLongToString(long long num)
{
   int counter = 0, i;
   char string[20];

   while(num !=0)
   {
      num = num / 10;
      counter ++;  
   } 
   

   for(i = counter; i=0; i--)
   {
      string[counter-1] = num/10;
      num = num / 10;
   }
   string[counter+1] = "\0";
   
   return string;
}

and i have following messege
invalid conversion from 'const char*' to 'char'
invalid conversion from 'char *' to 'char'
address of local variable 'string' returned

3
Contributors
2
Replies
3
Views
9 Years
Discussion Span
Last Post by Ancient Dragon
0

Model your code on something like say strcat(), where you pass the array of where you want to store the result.

0

1. functions can not return arrays like you are trying to do. A better solution is to pass a pointer to the array as a parameter so that the function can populate it, like this: void LongLongToString(long long num, char* string) 2. The entire algorighm for that function is just flat wrong. It can't work. Why? the value of num is 0 by the time line 22 is reached. What you want to do is make a copy of num so that its value can be restore before executing line 22. Another reason that algorithm is wrong is that line 24 will not insert the ascii character of the digit but rather the binary value, which is not what you want. You need to use the mod operator, not the division operator, and add '0' to the calculation string[counter-1] = (num %10) + '0';

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.