Concatenating arrays

Mar 1, 2011 at 10:48am
Hey guys.

I vaguely recall some way to temporarily concatenate arrays of the same type and was wondering if anyone knew either the syntax, of if I was imagining things...

For example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

int myFunction(char *buffer){
printf("%s", buffer);
}

int main(){
char array1[10];
sprintf(array1, "hello");

char array2[10];
sprintf(array1, "world");

myFunction(array1 + " " + array2);
// or maybe
myFunction(array1" "array2);
// or maybe
myFunction(array1." ".array2);

}


Obviously this wont compile and is syntactically wrong but I thought there might be something similar that would work as intended. Maybe all that PHP and Javascript I learned a few years ago is infecting my mind :P

In my head it I think it could never work unless the array addresses were sequential however I just wanted to know for sure :)

Thanks in advance.
Mar 1, 2011 at 12:44pm
I think you are confusing a few disparate things.

Adjacent string literals are automatically concatenated into a single string.
1
2
3
4
5
6
7
8
#include <stdio.h>

int main()
  {
  puts( "Hello" /* <-- auto concatenation here --> */ " world!" );

  return 0;
  }


In C, you must use the string concatenation functions.
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <stdio.h>
#include <string.h>

int main()
  {
  char        greeting_part_1[ 50 ] = "Hello";
  const char* greeting_part_2       = " world!";

  puts( strcat( greeting_part_1, greeting_part_2 ) );  /* result is cat'd string */
  puts( greeting_part_1 );  /* don't forget that the string was in fact modified */

  return 0;
  }


In C++, you can use the overloaded "+" operators to concatenate std::string and string literals.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <string>
using namespace std;

int main()
  {
  string greeting_part_1 = "Hello";

  cout << (greeting_part_1 + " world!") << endl;  // Again, the result is the concatenated string
  cout << greeting_part_1 << endl;  // but this time, the source string was not modified

  string greeting = greeting_part_1 + " world!";
  cout << greeting << endl;

  return 0;
  }

Hope this helps.
Mar 1, 2011 at 2:57pm

Thanks Duoas, it helped me as well in understanding the string concatenate .

I added few lines in your code to make it more easy for my comprehension.
With thanks for Duoas , I am appending / repeating his code here with slight additions:

( Tested in C++ only )



#include <string>
#include <iostream> // cout
#include <cstdlib> // itoa(), system()

using namespace std;
int main() {
string greeting_part_1 = "Hello";
string name ;
char *myAge;
int age ;
cout<< " Please write your name ";
cin>> name;
cout<< " What is your age ? ";
cin>> age;

// use itoa (IntegerToAlpha) to convert to string equivalent
itoa( age, myAge, 10 ); // the 10 is the number base

// myAge = String(age);

cout << (greeting_part_1 + " world!") << endl; // Again, the result is the concatenated string
cout << greeting_part_1 << endl; // but this time, the source string was not modified

string greeting = greeting_part_1 + " " + name + " you are in this world! for " + myAge + " years " ;
cout << greeting << endl;
system("pause");
return 0;
}

Mar 3, 2011 at 9:25am
Thanks guys this was tremendously helpful :) really apprieciate it!
Topic archived. No new replies allowed.