-5

Possible Duplicate:
How to convert integer to string in C?

I have a function in C which takes an int value i. I need to turn this i value into a string (char *).

Here is what I have so far:

char *str = (char *)i;
myfunction(str);

I get the error: cast to pointer from integer of different size.

Thanks!

Community
  • 1
  • 1
user92390
  • 33
  • 1
  • 3

1 Answers1

0

This is just not valid code. Even if you resolve the compile-time error, the result will likely be some sort of memory access error at run time. The result will be a pointer to a string that you have no idea where it actually points to. And so you'll have an error when your program accesses that memory.

I'm going to speculate that what you are really looking for is a function like itoa(). That's the only way this would make any sense.

Edit

Based on a new comment, I see that you do in fact want a function like itoa(). Simply trying to convert the integer to a string pointer will not do what you want.

char buff[80];

itoa(i, buff, 10);
myfunction(buff);
Jonathan Wood
  • 65,341
  • 71
  • 269
  • 466
  • I am not suppose to use outside functions. I need to be able to print the integer character by character – user92390 Jan 27 '13 at 02:33
  • 1
    Sheesh, it's a lot of work trying to find out exactly what you are trying to do. You should've posted the code for `myfunction()`. So then I guess you'll need to write your own `itoa()` function. I've done so many times myself. But you'll need to understand the C language a little better. – Jonathan Wood Jan 27 '13 at 02:37
  • sorry my apologies. myfunction(char *string) simply goes one by one through the string and prints each character – user92390 Jan 27 '13 at 02:40
  • It's valid code, although results may vary, a lot. – netcoder Jan 27 '13 at 02:46
  • It's not valid code to take an integer, convert it to a char *, and then expect it to point to a string version of the original integer value. – Jonathan Wood Jan 27 '13 at 02:52