ADVERTISEMENT

Difference between char [] and char * in C?

mycplus
ADVERTISEMENT

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

char s_name[] = "mycplus";
char *p_name  = "mycplus";

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.

#include <stdio.h>

int main()
{
    char s_name[] = "Hello, World!"; 
    char *p_name = "Hello, World!"; 
    
    printf("The size of s_name[] is %lu\n",sizeof(s_name));
    printf("The size of p_name[] is %lu\n",sizeof(p_name));
    
    printf("Print s_name[]: %lu\n", s_name);
    printf("Print &s_name[]: %lu\n", &s_name);
    
    printf("Print p_name[]: %lu\n", p_name);
    printf("Print &p_name[]: %lu\n", &p_name);
    
    p_name="Program";
    //s_name="Program";
    
    return 0;
}

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*’
The size of s_name[] is 14
The size of p_name[] is 8
Print s_name[]: 140723957079242
Print &s_name[]: 140723957079242
Print p_name[]: 4196116
Print &p_name[]: 140723957079232

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.

01. error: assignment to expression with array type
02. error: incompatible types in assignment of ‘const char [8]’ to ‘char [14]’
03. error: array type 'char [14]' is not assignable
ADVERTISEMENT
M. Saqib: Saqib is Master-level Senior Software Engineer with over 14 years of experience in designing and developing large-scale software and web applications. He has more than eight years experience of leading software development teams. Saqib provides consultancy to develop software systems and web services for Fortune 500 companies. He has hands-on experience in C/C++ Java, JavaScript, PHP and .NET Technologies. Saqib owns and write contents on mycplus.com since 2004.
Related Post