13

Is there a command that can append one array of char onto another? Something that would theoretically work like this:

//array1 has already been set to "The dog jumps "
//array2 has already been set to "over the log"

append(array2,array1);
cout << array1;

//would output "The dog jumps over the log";

This is a pretty easy function to make I would think, I am just surprised there isn't a built in command for it.

*Edit

I should have been more clear, I didn't mean changing the size of the array. If array1 was set to 50 characters, but was only using 10 of them, you would still have 40 characters to work with. I was thinking an automatic command that would essentially do:

//assuming array1 has 10 characters but was declared with 25 and array2 has 5 characters
int i=10;
int z=0;    
do{
    array1[i] = array2[z];
    ++i;
    ++z;
}while(array[z] != '\0');

I am pretty sure that syntax would work, or something similar.

1
  • There is no single complete type "array" in C++. Commented Mar 31, 2012 at 11:03

4 Answers 4

19

If you are not allowed to use C++'s string class (which is terrible teaching C++ imho), a raw, safe array version would look something like this.

#include <cstring>
#include <iostream>

int main()
{
    char array1[] ="The dog jumps ";
    char array2[] = "over the log";
    char * newArray = new char[std::strlen(array1)+std::strlen(array2)+1];
    std::strcpy(newArray,array1);
    std::strcat(newArray,array2);
    std::cout << newArray << std::endl;
    delete [] newArray;
    return 0;
}

This assures you have enough space in the array you're doing the concatenation to, without assuming some predefined MAX_SIZE. The only requirement is that your strings are null-terminated, which is usually the case unless you're doing some weird fixed-size string hacking.

Edit, a safe version with the "enough buffer space" assumption:

#include <cstring>
#include <iostream>

int main()
{
    const unsigned BUFFER_SIZE = 50;
    char array1[BUFFER_SIZE];
    std::strncpy(array1, "The dog jumps ", BUFFER_SIZE-1); //-1 for null-termination
    char array2[] = "over the log";
    std::strncat(array1,array2,BUFFER_SIZE-strlen(array1)-1); //-1 for null-termination
    std::cout << array1 << std::endl;
    return 0;
}
Sign up to request clarification or add additional context in comments.

3 Comments

The question is about appending second array to first array, but you made a new array!
@MasoudM. the original question stated that two char arrays had to be added. There was no clear requirement of array reuse. Even with this requirement, this is a decent answer, because the "fixed buffer size" is simply not done in C++ (actually raw arrays in general). I added a safer version with the added requirements.
Ok, you edited your answer. but the original question was "Is there a command that can append one array of char onto another?"
5

If your arrays are character arrays(which seems to be the case), You need a strcat().
Your destination array should have enough space to accommodate the appended data though.

In C++, You are much better off using std::string and then you can use std::string::append()

2 Comments

@TaylorHuston: I suspected so,Most of the assignment and homework related Q's have some strange stipulations of not using the features in C++ which form the very basis of C++,& that is the reason I provided both the alternatives.strcat() or strncat() is what you should use in that case.Check out the link for usage details.
All these years, and still nobody has pointed out that when working in embedded environments the use of strings is considered harmful. And for very good reasons.
2

You should have enough space for array1 array and use something like strcat to contact array1 to array2:

char array1[BIG_ENOUGH];
char array2[X];
/* ......             */
/* check array bounds */
/* ......             */

strcat(array1, array2);

Comments

1

There's no built-in command for that because it's illegal. You can't modify the size of an array once declared.

What you're looking for is either std::vector to simulate a dynamic array, or better yet a std::string.

std::string first ("The dog jumps ");
std::string second ("over the log");
std::cout << first + second << std::endl;

3 Comments

You can't even modify the size of an array before it's declared. The size is part of the type. What you're saying is like saying that "you can't change an int to a float after it's declared".
@KerrekSB An array type without a size is incomplete, but it's still a type. However, in that case you can modify the size once declared :D . Seems clear enough what Luchian is saying: a well-defined array object has an immutable size.
@Potatoswatter: You're right, I forgot that you can actually declare an incomplete type with extern, as in extern int a[];. Good point. (But you can't modify the size, only specify it. That is, you can merely defer the decision.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.