c++ char data type
Im wondering if there''s any way of adding two chars, like:
char *one = "hello";
char *two = " !";
one = one + two;
Which would result in that one = "hello !"...
I know I can do that with strings, but that was not the question.
use STL string:
string str1="Hello";
string str2="!";
string str=str1+str2+" haha, it works";
-----------
RacingTreme - for fun multiplayer racing:
http://users.utu.fi/stkibr
string str1="Hello";
string str2="!";
string str=str1+str2+" haha, it works";
-----------
RacingTreme - for fun multiplayer racing:
http://users.utu.fi/stkibr
I just wrote I knew how to do it with strings... I cant use strings for this solution...
simple 
char *one = "hello";
char *two = "!";
char final[10]; // char array to store final string in
strcpy(final, one); // copy the first part of final string
strcat(final, two); // add on last part
one thing, u may have to include a header file, im not sure which one though

char *one = "hello";
char *two = "!";
char final[10]; // char array to store final string in
strcpy(final, one); // copy the first part of final string
strcat(final, two); // add on last part
one thing, u may have to include a header file, im not sure which one though
That seemed to work dude
Tnx alot 
Know of any other strcpy-style functions?


Know of any other strcpy-style functions?
September 17, 2002 09:33 AM
Just a little note... It''s safer to use the strn* functions because you specify the size to copy etc... Go to google and type "c reference" or something similar and you''ll find a handful of pages with complete c references. You said you were using c++ but the str and strn functions are primarily used in c (along with char*).
Here is and article about c string functions and how to avoid buffer overflows. You should check it out - it''s really good.
http://www-106.ibm.com/developerworks/library/buffer-defend.html
Hope this helps,
Matt
Here is and article about c string functions and how to avoid buffer overflows. You should check it out - it''s really good.
http://www-106.ibm.com/developerworks/library/buffer-defend.html
Hope this helps,
Matt
I''ve got a problem concerning char too. I thought i could post my querstion here, so i don''t create another thread with the same subject.
I am developing a console-chat application. When the user press down any key on the keyboard, the program shall read the keycode, and print it to screen. I use this code:
----------------begin
int inputChar;
static char sOutput[100];
char *sInput;
inputChar = getch();
sInput = (char*)inputChar;
strcpy(sOutput,sInput);
----------------end
inputChar get the keycode for the pressed key.
sInput = (char*)inputChar
is ment to convert the number in inputChar to into a character (for example, 65=A).
strcpy(Output,sInput) is made to copy he value of sInput into sOutput.
But...it doesnt work :/
Anyone knows why? Is sInput = A after
sInput = (char*)inputChar
? Or what is the problem?
Thanks in advance, i really need some help with this...
I am developing a console-chat application. When the user press down any key on the keyboard, the program shall read the keycode, and print it to screen. I use this code:
----------------begin
int inputChar;
static char sOutput[100];
char *sInput;
inputChar = getch();
sInput = (char*)inputChar;
strcpy(sOutput,sInput);
----------------end
inputChar get the keycode for the pressed key.
sInput = (char*)inputChar
is ment to convert the number in inputChar to into a character (for example, 65=A).
strcpy(Output,sInput) is made to copy he value of sInput into sOutput.
But...it doesnt work :/
Anyone knows why? Is sInput = A after
sInput = (char*)inputChar
? Or what is the problem?
Thanks in advance, i really need some help with this...
Try using this:
int inputChar;
static char sOutput[100];
char sInput[2];
inputChar = getch();
sInput[0] = (char)inputChar;
sInput[1] = 0;
strcpy(sOutput,sInput);
When you use sInput = (char*)inputChar
sInput is not storing the value ''A'' or whatever value you entered, but a pointer to whatever is stored at a memory location of 65. Also you must pass null terminated strings to strcpy otherwise strcpy will not know when to stop copying.
Also I think you may want to use strcat instead of strcpy. strcpy will replace whatever is in sOutput, strcat will add to it.
Hope this helps.
The difference between us and a computer is that, the computer is blindingly stupid, but it is capable of being stupid many, many million times a second.
-Douglas Adams
int inputChar;
static char sOutput[100];
char sInput[2];
inputChar = getch();
sInput[0] = (char)inputChar;
sInput[1] = 0;
strcpy(sOutput,sInput);
When you use sInput = (char*)inputChar
sInput is not storing the value ''A'' or whatever value you entered, but a pointer to whatever is stored at a memory location of 65. Also you must pass null terminated strings to strcpy otherwise strcpy will not know when to stop copying.
Also I think you may want to use strcat instead of strcpy. strcpy will replace whatever is in sOutput, strcat will add to it.
Hope this helps.
The difference between us and a computer is that, the computer is blindingly stupid, but it is capable of being stupid many, many million times a second.
-Douglas Adams
The difference between us and a computer is that, the computer is blindingly stupid, but it is capable of being stupid many, many million times a second.-Douglas Adams
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement