Holloweye Posted February 15, 2010 Share Posted February 15, 2010 In C not C++. 1. How can I add a int with a char? Like: int x = 5; char a; a = x; 2. How can I add 2 char's to one char? Like: char a[]; a = "HELLO "; char b[]; b = "WORLD"; a += b; Thanks Quote Link to comment Share on other sites More sharing options...
Masterxilo Posted February 15, 2010 Share Posted February 15, 2010 1. How can I add a int with a char? Like: int x = 5; char a; a = x; How is that an addition? Do you mean you want to assign an int to a char? Then your code should work. You can also do an explicit conversion: int x = 5; char a; a = (char)x; For 2.: char a[80]; strcpy(a, "HELLO "); strcat (a, "WORLD"); Quote Hurricane-Eye Entertainment - Site, blog. Link to comment Share on other sites More sharing options...
Holloweye Posted February 16, 2010 Author Share Posted February 16, 2010 But if I try to do this: int x = 5; char a[9]; strcpy(a,"SCORE "); strcat(a,(char*)x); I dont get any errors but after a while the program crash. I guess the error is when trying copy the x variable to a. Anyone know what could fix this? Quote Link to comment Share on other sites More sharing options...
Niosop Posted February 16, 2010 Share Posted February 16, 2010 You want either itoa or sprintf to convert the integer 5 to the string "5". As it is you're telling it to try and make a string out of whatever is at a memory location, which will cause a crash. Probably something like this: int i = 5; char a[30]; strcpy(a, "SCORE "); char buffer[30]; itoa (i,buffer,10); strcat(a, buffer); Quote Windows 7 x64 - Q6700 @ 2.66GHz - 4GB RAM - 8800 GTX ZBrush - Blender Link to comment Share on other sites More sharing options...
Holloweye Posted February 16, 2010 Author Share Posted February 16, 2010 You want either itoa or sprintf to convert the integer 5 to the string "5". As it is you're telling it to try and make a string out of whatever is at a memory location, which will cause a crash. Probably something like this: int i = 5; char a[30]; strcpy(a, "SCORE "); char buffer[30]; itoa (i,buffer,10); strcat(a, buffer); Error: main.c:81: warning: implicit declaration of function 'itoa' I have #stdlib.h and string.h included. I guess there is no such function in C. Quote Link to comment Share on other sites More sharing options...
Canardia Posted February 16, 2010 Share Posted February 16, 2010 int i = 5; char a[30]; strcpy(a, "SCORE "); char buffer[30]; itoa (i,buffer,10); strcat(a, buffer); That can be written much shorter: int i=5; char buffer[30]; sprintf(buffer,"SCORE %d",i); Quote ■ Ryzen 9 ■ RX 6800M ■ 16GB ■ XF8 ■ Windows 11 ■ ■ Ultra ■ LE 2.5 ■ 3DWS 5.6 ■ Reaper ■ C/C++ ■ C# ■ Fortran 2008 ■ Story ■ ■ Homepage: https://canardia.com ■ Link to comment Share on other sites More sharing options...
Holloweye Posted February 16, 2010 Author Share Posted February 16, 2010 That can be written much shorter: int i=5; char buffer[30]; sprintf(buffer,"SCORE %d",i); It worked! Thank you very much! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.