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.

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
{
    private:
        char str[50],str1[50];
        float h;
        public:
            st()
            {
                cout<<"enter your name = ";
                cin.getline(str,50);
                cout<<"enter your friend name = ";
                cin.getline(str1,50);
                cout<<"enter your height = ";
                cin>>h;
            }
            void show()
            {
                cout<<"\n\n";
                cout<<"your name is = "<<str<<endl;
                cout<<"height is = "<<h<<endl;
                cout<<"your Friend name is = "<<str1<<endl;
            }
};
int main() 
{   
    st x,y,z;
    x.show();
    y.show();
    z.show();
    getch();
    return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.