Jump to content
  • Advertisement


This topic is now archived and is closed to further replies.



This topic is 5880 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello! I am trying to "convert" a integer to a string so i can print it with TextOut(); However, when i compile, i dont get any errors, but the program terminates imediatly. It runs fine however if i comment the sprintf stuff. This is how im doing it.
// the integer i want to make a string of.

int a_int = 5;

// the charstring.

char *a_char;

// this is how im trying to do it. i have included stdio.h

sprintf(a_char, "%s", a_int);

// this however makes my program terminate, but if i understand the docs right, this is the way to do it! =(

Thanks in advance!

Share this post

Link to post
Share on other sites
hi olle

you must allocate memory for your string

a_char = (char *) malloc (16); // reserves 16 bytes which is sufficient

then use


see sprintf (msdn)

dont forget to "free(a_char);"

[edited by - diego_rodriguez on September 10, 2002 11:12:34 AM]

Share this post

Link to post
Share on other sites
It seems you don''t allocate any memory for your
temporary string a_char. A pointer without
allocation is useless.

If you don''t need a_char in the rest of your
program, you''d better define a array of char
like this :

int i = 123;
char temp_str[80]; // change 80 with the number of characters you need
sprintf( temps_str, "the INT i want to disPLAY: %i", i);

Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!