How to convert int to char * ?

K

Kay

how to convert int to char * ?

I have found this in a web site. However, it doesn't work even I change
itoa to atoi including stdlib.h

char str[10];
int i=567;

str=itoa(i, str, 10);
 
M

Mike Wahler

Kay said:
how to convert int to char * ?

An integer type cannot be portably converted to a pointer.
Perhaps you want to create the textual representation of
an integer, e.g. "123" and have a 'char*' object point to it?
I have found this in a web site.

Which shows the danger of looking to 'a web site' for
accurate information. You should have at least one (preferably
two or more) books for learning C. See www.accu.org for
peer reviews and recommendations. Also see the C FAQ:
http://www.eskimo.com/~scs/C-faq/top.html
and the comp.lang.c 'welcome message':
http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html
...both of which contain links to useful information about C.
However, it doesn't work even I change
itoa to atoi including stdlib.h

char str[10];
int i=567;

str=itoa(i, str, 10);

There is no function 'itoa()' in the C standard library
(though some implementations might provide such a function as an
extension). The C standard library does have a function
'atoi()', but it converts a the textual representation
of an integer to an integer.

You won't get far by just guessing and changing things
blindly, hoping it will work.

If you want to create the textual representation of an
integer, use 'sprintf()':

int i = 123; /* create an integer object */
char text[20]; /* create a place to store text */
sprintf("%d", text); /* create textual representation of 'i' */
/* and store it in the array 'text' */
char *p = text; /* create a pointer and assign it the address of */
/* the first character of the array 'text' */

Note that a pointer alone is not sufficient, you need an actual
place to store the characters (here, the array 'text').

-Mike
 
M

Mike Wahler

Mike Wahler said:
int i = 123; /* create an integer object */
char text[20]; /* create a place to store text */
sprintf("%d", text); /* create textual representation of 'i' */

Typo, should be:

sprintf(text, "%d", i);

Sorry about that.

-Mike
 
A

Al Bowers

Mike said:
how to convert int to char * ?


If you want to create the textual representation of an
integer, use 'sprintf()':

int i = 123; /* create an integer object */
char text[20]; /* create a place to store text */
sprintf("%d", text); /* create textual representation of 'i' */

The statement should be:
sprintf(test,"%d",i);
/* and store it in the array 'text' */
char *p = text; /* create a pointer and assign it the address of */
/* the first character of the array 'text' */

Example:

#include <stdio.h>

int main(void)
{
char *p, text[32];
int i = 123;

sprintf(text,"%d",i);
p = text;
printf("p points to the string: \"%s\"\n",p);
return 0;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,146
Messages
2,570,832
Members
47,374
Latest member
anuragag27

Latest Threads

Top