deference b/n char array and strings

hello guys

is their deference between character arrays and strings ?
ans with example!
tanks!
yes.

a char array is just an array of bytes. It has no methods, you must use functions that expect it to be in a certain format (eg strlen() instead of object.size() ). Char arrays are how C does strings and they work in c++ but have numerous problems (error prone being one of them).

a string is a full on stl container with algorithms (find, etc) and tools (size(), etc) built in as well as a real assignment operator (can't just say x=y for arrays, have to use memcpy or strcpy), real comparisons (can't compare arrays either...) and more.

honestly, the only thing they have in common is both of them store a block of bytes that happen to end in a zero in a sequential block of memory.

try to avoid using char arrays. If you encounter old code that requires it, you can still use strings mostly, as you can directly assign a string from a char array result and you can feed a string to a char array with a method.
Last edited on
It depends on what strings you are talking about.

If you meanstd::string, then character arrays are indeed different. The former is merely a class defined within the standard library which represents string data.

But, functionally, they are the same thing because they both support the same operations, except that character arrays support them because they are an integral type, while std::strings support them because of operator overloading.
By the way a char array is not necessarily a C-string. To be a C-string the array of char must be properly terminated with the end of string character ('\0').

Topic archived. No new replies allowed.