Assalam o Alikum my question is that what is the difference between character array and string. my instructor is use
char str [50] instead of string str. and i can not understand what is difference between them.

3 Years
Discussion Span
Last Post by moaz.amin.37

std::string maintains memory (and various other operators) for you.

Everything you do with a char array must be handled by you explicitly: memory management, copy, appending, length checking, and so on.


A character array is just what it sounds like, it's a raw array (sequence of contiguous memory) of characters (1 byte numbers that are interpreted as characters when printed on a screen) with the end marked with a 0 character (null-terminated strings). That's about all you get with a character array, it's just raw memory and you have to manually allocate / deallocate it, use C-style functions like strcmp or strcat to do operations on that memory, and you have to worry about capacity. Character arrays are essentially the only option in C for representing strings. It can be useful to know about them and how to work with them, mainly as a way to build your "low-level understanding" of things, but they are not really used in practice in C++, unless you are dealing with some C code.

Strings, as in, using the std::string class, are the proper way to deal with strings in C++. This is a class that encapsulates a dynamic array of characters and provides a number of useful features to manipulate strings. For one, it automatically handles the allocation / deallocation of the memory, as well as increasing the capacity whenever needed (as you append stuff to the string). That, by itself, is a sufficient reason to use C++ strings over the C-style character arrays, almost always. In addition, C++ strings have a number of nice functions and operators that allow you to compare, concatenate, split, and transform the strings very easily and safely. They are, by far, the preferred option.

Some instructors like to use character arrays for one of two reasons: (1) they want you to understand the underlying "raw" mechanisms before you start using std::string; or, (2) they are old and back when they learned C++ (probably before the standard was established), the std::string class was a new and exotic thing. If it's the former, then that's fine, it can be good to learn this low-level stuff (but it's not good to dwell too much on it either). If it's the latter, then I have to emit the following:

Warning: Use of 'your instructor' is deprecated. Consider a modern replacement for 'your instructor'.

run this program there is problem in it

#include <iostream>
#include <conio.h>
#include <string.h>
using namespace std;
class st
        char str[50],str1[50];
        float h;
                cout<<"enter your name = ";
                cout<<"enter your friend name = ";
                cout<<"enter your height = ";
            void show()
                cout<<"your name is = "<<str<<endl;
                cout<<"height is = "<<h<<endl;
                cout<<"your Friend name is = "<<str1<<endl;
int main() 
    st x,y,z;
    return 0;
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.