473,398 Members | 2,389 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

How to convert int to char * ?

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);

Nov 14 '05 #1
4 79395
Kay <er*********@yahoo.com.hk> spoke thus:
I have found this in a web site. However, it doesn't work even I change
itoa to atoi including stdlib.h
Of course not; what do you think atoi() does? (Hint: strtol() does it
better.)
char str[10];
int i=567; str=itoa(i, str, 10);


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

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #2
"Kay" <er*********@yahoo.com.hk> wrote in message
news:41************@yahoo.com.hk...
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/bchambl...me_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
Nov 14 '05 #3

"Mike Wahler" <mk******@mkwahler.net> wrote in message
news:_J***************@newsread3.news.pas.earthlin k.net...

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
Nov 14 '05 #4


Mike Wahler wrote:
"Kay" <er*********@yahoo.com.hk> wrote in message
news:41************@yahoo.com.hk...
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;
}
--
Al Bowers
Tampa, Fl USA
mailto: xa******@myrapidsys.com (remove the x to send email)
http://www.geocities.com/abowers822/

Nov 14 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Markus Hämmerli | last post by:
I ' ll tra to convert a Cstring to char* without success. I working in a Unicode enabled environment this is working in Unicode CString source = _T("TestString"); TCHAR *szSource =...
1
by: Sam Smith | last post by:
Hi, I wan't a function to take a const char*, a start bit position and number of bits and convert that bit-stream into a primitive of desired type. I.e. something like: char convert(const...
5
by: Brad Moore | last post by:
Hey all, I'm getting the following compiler error from my code. I was wondering if anyone could help me understand the concept behind it (I actually did try and compile this degenerate...
7
by: whatluo | last post by:
Hi, all I'm now working on a program which will convert dec number to hex and oct and bin respectively, I've checked the clc but with no luck, so can anybody give me a hit how to make this done...
7
by: MilanB | last post by:
Hello How to convert char to int? Thanks
15
by: Yifan | last post by:
Hi Does anybody know how to convert System::String* to char*? I searched the System::String class members and did not find any. Thanks Yifan
19
by: caramel | last post by:
i've been working on this program forever! now i'm stuck and going insane because i keep getting a syntax error msg and i just can't see what the compiler is signaling to! #include <stdio.h>...
3
by: Eric BOUXIROT | last post by:
hi, i must convert all of these eVC++ prototypes to use with VB.NET.... DLLEXPORT long F_BDO_MessageBoxOK(char *IN_title, char *IN_msg ); DLLEXPORT long F_BDO_MessageBoxOUINON(char *IN_title,...
12
by: GRoll35 | last post by:
I get 4 of those errors. in the same spot. I'll show my parent class, child class, and my driver. All that is suppose to happen is the user enters data and it uses parent/child class to display...
5
by: Zytan | last post by:
I am surprised that a single character string is not auto-created from a single char. It is hard to find information on converting a char into a string, since most people ask how to convert char...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.