Re: [Dev-C++] Casting of int to string
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Per W. <pw...@ia...> - 2007-05-07 08:22:29
|
Better C libraries have the snprintf() function, that takes the size of the buffer as an input parameter. /pwm On Mon, 7 May 2007, Philip Bennefall wrote: > 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 ----- > From: Jonathan Winterflood > To: Jan Mura > Cc: dev...@li... > 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. > 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. > > 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 > 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 > ----- 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 > > do > > it. Should I use & operator and adress of int in teh function? Or > > something > > else? > > > > Thank you very much > > > > Jan Mura > > > > > > ------------------------------------------------------------------------- > > 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 > > > > > ------------------------------------------------------------------------- > 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 > > > > > -- > <Morpheus> linux, c'est une question de VI ou de MORE > > > ------------------------------------------------------------------------------ > > > ------------------------------------------------------------------------- > 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 > |