Generally, the following two statements about char[] and char * in C are considered confusing and understanding the difference between them is important.

The first statement puts the literal string “mycplus” in read-only memory and copies the string to newly allocated memory on the stack. This allows you to modify the contents of the array which is holding the string, and it is statically allocated.

The second statement is known as static string allocation and definition. This statement places the string  “mycplus” in the read-only parts of the memory and making p_name  a pointer to that string. Also, any writing operation on this memory location are illegal. However, the pointer itself can be changed to point to a new location.

Some of the differences between char [] and char * are as follow:

  1. Main difference between both the statements is that s_name is an array where as p_name is a pointer type variable.
  2. The sizeof(s_name) and sizeof(p_name) are different because memory handling is different in both statements as explained above.
  3. Variable s_name and it’s address &s_name is same. where as p_name and &p_name are not same.
  4. The statement s_name=”Program” is invalid while p_name=”Program” is a invalid C statement.

Above statements can be verified using the following C Program.

The output of the program looks similar to the following (depending upon compiler and OS used). The program might show some warnings such as ISO C++ forbids converting a string constant to ‘char*’

If you try to un-comment the statement //s_name="Program"; the program will terminate and show an error message similar one of the following.