@
elsa
If you can avoid
char[] arrays then please do as
salem suggests.
However, if you cannot use
std::string for your assignment, then:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
|
int main()
{
char fullName [ 100 ]; // enough chars to hold all parts of the name plus a space
char firstAndMiddle[ 50 ]; // enough for a first and middle name
char lastName [ 50 ]; // enough for a last name
cout << "Enter your first and middle names: ";
cin.getline( firstAndMiddle, sizeof(firstAndMiddle) );
cout << "Enter your last name: ";
cin.getline( lastName, sizeof(lastName) );
// strcpy() simply copies a char array until it hits (and copies) a zero value (null, or '\0')
// It does not care what is in the target array -- it just writes over it.
// You must make sure that the target array is big enough to hold the source data.
strcpy( fullName, firstAndMiddle );
// strcat() is similar, but it first finds the end of the target array (finds the zero),
// then starts copying from where the zero is.
// Again, you must make sure that there is enough room left in the target array to
// hold both its current content and the additional data in the source.
strcat( fullName, " " ); // (first we will add that space between the names)
strcat( fullName, lastName ); // (then we will add the last name)
cout << fullName << "\n";
}
|
Using
char[] arrays is tricky on a number of levels.
The first and most important is to make sure you have enough space to copy things around, and to keep your strings null-terminated, for which there must also be space. So, if you say
char s[ 100 ]
then you have a string big enough to hold a maximum of 99 characters + 1 null terminator (zero). An empty string would have
s[0] = '\0';
.
In the example above, you have two arrays holding up to 49 characters each. 49 + 49 is 98. Plus a space = 99. The combined string needs to be at least 100 elements long (99 characters + 1 null terminator).
Another useful thing, which I did in the code above, is use the
sizeof operator to help track the number of elements in the array. It only works on ARRAYS. Not pointers!
What this means that I only need to state the array's size
once, where I declare it:
1 2
|
char s[ 100 ];
cout << "s has " << sizeof(s) << " characters.\n";
|
But I cannot use it the same way with a
char*:
1 2 3 4 5 6 7 8 9 10 11 12 13
|
char* s;
int n = 100;
s = malloc( n );
cout << "s is a pointer. The pointer's size is " << sizeof(s) << " bytes.\n";
cout << "I do not know how many characters s can hold.\n";
cout << "(Unless I keep track. The number I used with malloc() is " << n << ",\n";
cout << "so s has " << n << " characters.\n";
// Don't forget me!
free( s );
|
It also doesn't work with arrays of things larger than one byte wide:
1 2 3 4 5 6
|
int xs[ 100 ];
cout << "xs is " << sizeof(xs) << " bytes long.\n";
// We need an additional trick to determine the number of elements:
cout << "xs has " << (sizeof(xs) / sizeof(x[0])) << " elements.\n";
|
The same caveat as above applies with malloc()ed arrays of stuff:
1 2 3 4 5 6 7 8 9 10 11
|
int* xs;
int n = 100;
xs = malloc( n * sizeof(int) );
cout << "*xs has n elements (n = " << n << ").\n";
cout << "which means that *xs is " << (n * sizeof(xs[0])) << " bytes long.\n";
cout << "xs itself (a pointer) is " << sizeof(xs) << " bytes.\n";
...
free( xs );
|
Hope this helps.