Posted by Ali Bhai on 1999-04-19
> char *ByteToString (unsigned char Input);
I worked on your problem and could find out only one solution (funny isn't
it) -
#include <stdio.h>
char result[11];
char *stupid (char input)
{
int i;
for (i=0; i<10; i++)
result[i] = input;
result[i] = '\0';
return result;
}
int main (void)
{
char *output;
printf ("Result: %s\n", stupid('a'));
output = stupid ('b');
printf ("Result 2: %s\n", output);
return 0;
}
You have to declare the result array (actually string) used in "stupid" as
global. If you define it as local the pointer will point to garbage on exit.
When a function returns, its local variables are destroyed and so will be
done with result (if it is declared inside the stupid function).
There is one more solution. If you change the definition of the stupid
function to "void stupid (char input, char *output)", then there will be no
problem.
You said that your function "char *ByteToString (char input)" worked with
printf. I can't understand how this is possible. I have tried to do this
with GCC and TC, but the result is the same (i.e. the returned pointer
points to garbage). A function can't return a pointer to a local variable in
my opinion. Will you please like to pass on the code of such a function?
Ali Bhai
BCS III - FAST Institute of Computer Science, Karachi
email: mashah@xxxxx.xxx.xx
web page: www.pak.org/mufta
<----------------------------------------->
[To err is human but to really mess up the things you need a computer.]
Previous post | Next post | Timeline | Home