how to initialize a char pointer in a class?
<p>I have a confusion while dealing with char pointers. Please have a look at following code:</p>

<pre><code>class Person
{
    char* pname;
public:
    Person(char* name)
    {
        //I want to initialize 'pname' with the person's name. So, I am trying to
        //achieve the same with different scenario's

        //Case I:   
        strcpy(pname, name); // As expected, system crash.

        //Case II: 
        // suppose the input is "ABCD",  so trying to create 4+1 char space
        // 1st 4 for holding ABCD and 1 for '\0'.
        pname = (char*) malloc(sizeof(char) * (strlen(name)+1) );
        strcpy(pname, name);

        // Case III: 
        pname = (char*) malloc(sizeof(char));
        strcpy(pname, name);
    }

    void display()
    {
        cout&lt;&lt;pname&lt;&lt;endl;
    }
};

void main()
{
    Person obj("ABCD");
    obj.display(); 
}
</code></pre>

<p>For Case I:
As expected, system crash.</p>

<p>Output for Case II:</p>

<blockquote>
  <p>ABCD</p>
</blockquote>

<p>Output for Case III:</p>

<blockquote>
  <p>ABCD</p>
</blockquote>

<p>So, I am not sure why Case II &amp; III are producing the same output !!!!.....
How I should initialize a char pointer in a class?</p>
