adding to char arrays together for one arg
-
I've been using c++ for two weeks now and i've got a good grip on coding by myself but... is it possible to call a function like this: char* myFunc(char* input) { printf(input); } int main(void) { char* one = "hello "; char* two = "world!"; myFunc(one+two); } this is of the top of my head but can i add two strings together for a function? Even if there is away around this would be great. Thanks
-
I've been using c++ for two weeks now and i've got a good grip on coding by myself but... is it possible to call a function like this: char* myFunc(char* input) { printf(input); } int main(void) { char* one = "hello "; char* two = "world!"; myFunc(one+two); } this is of the top of my head but can i add two strings together for a function? Even if there is away around this would be great. Thanks
Roman957 wrote:
but can i add two strings together for a function?
No. There's not a + operator that will concatenate two char strings. You'd need to do that manually - here's one way:
int main(void)
{
char* one = "hello ";
char* two = "world!";char buffer[16];
strcpy(buffer, one); // copy string "one" to buffer
strcat(buffer, two); // append string "two" to buffermyFunc(buffer);
}Mark
"Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder
-
I've been using c++ for two weeks now and i've got a good grip on coding by myself but... is it possible to call a function like this: char* myFunc(char* input) { printf(input); } int main(void) { char* one = "hello "; char* two = "world!"; myFunc(one+two); } this is of the top of my head but can i add two strings together for a function? Even if there is away around this would be great. Thanks
It may count as advanced at this stage of learning (getting arrays and pointers in your head is very handy!), but there are classes that encapsulate strings, and you can then use
+
eg: std::string one = "hello"; std::string one = "world"; somefunc (one + two); Also look at CString if you're using MFC. Iain.