Re: [Dev-C++] Casting of int to string
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Philip B. <phi...@pb...> - 2007-05-07 07:30:39
|
Sorry, I should've phrased it differently. Yup, allocating too little =
memory is a bad idea in any situation because the function has no way of =
knowing when it is supposed to stop writing. I just put 32 in there =
because it ensures (100 %) that the entire value will be stored =
including the Null terminator.
Regards
Philip Bennefall
----- Original Message -----=20
From: Jonathan Winterflood=20
To: Jan Mura=20
Cc: dev...@li...=20
Sent: Monday, May 07, 2007 9:08 AM
Subject: Re: [Dev-C++] Casting of int to string
Just a note:
""" It basically prints your integer into the character array, as long =
as there is enough space to hold it """
Actually it will print the integer whether there is enough space or =
not.. It is entirely Your job to make sure there is enough space, or =
else sprintf will just keep on printing in memory after the end of the =
array. Obviously this is a very bad idea, since there is other data =
there.=20
In this case, determining the necessary space is as easy as 'what's =
the maximum digit count in an integer'... but in other cases, you can =
get wierd crashes, wrong data for no apparent reason, or even create an =
exploit in your program.=20
Thought you might want to know that (and if you did, no offense =
intended :)
Jonathan
On 5/7/07, Philip Bennefall < phi...@pb...> wrote:
This will do the job.
char outputstring[32];
sprintf(outputstring, "%d", integer);
It basically prints your integer into the character array, as long =
as there
is enough space to hold it. This method works both in C and C++. =
There are=20
other methods such as stringstream, but they only work in C++. If =
you're
compiling a C program, for this function to work you need to include =
stdio.h
and for C++ it is cstdio.
Regards
Philip Bennefall=20
----- Original Message -----
From: "Jan Mura" <jan...@vo...>
To: <dev...@li... >
Sent: Monday, May 07, 2007 8:26 AM
Subject: [Dev-C++] Casting of int to string
> Hello,
>
> I would like to compare an integer with a string using strcmp(). =
So I need
> to cast the int type to string (*char). Is there some commone way =
how to=20
> do
> it. Should I use & operator and adress of int in teh function? Or
> something
> else?
>
> Thank you very much
>
> Jan Mura
>
>
> =
-------------------------------------------------------------------------=
=20
> This SF.net email is sponsored by DB2 Express
> Download DB2 Express C - the FREE version of DB2 express and take
> control of your XML. No limits. Just data. Click to get it now.
> http://sourceforge.net/powerbar/db2/
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...=20
> TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users=20
>
=
-------------------------------------------------------------------------=
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take
control of your XML. No limits. Just data. Click to get it now.=20
http://sourceforge.net/powerbar/db2/
_______________________________________________
Dev-cpp-users mailing list
Dev...@li...
TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm
https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
--=20
<Morpheus> linux, c'est une question de VI ou de MORE=20
-------------------------------------------------------------------------=
-----
=
-------------------------------------------------------------------------=
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take
control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/
-------------------------------------------------------------------------=
-----
_______________________________________________
Dev-cpp-users mailing list
Dev...@li...
TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm
https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
|