char* VS char[]

  1. Conclusion
    1. Implementation
      1. Method 1: char*
        1. Method 2: char[]
      2. Array decay
      3. Dynamic memory
      4. References

Conclusion

char* and char[] are different types in most cases, but it’s not immediately apparent in all cases. Essentially, char* means a pointer to a constant stored in the .data segment, char[] is a character array data type.

More information about the difference between array and pointer here.

Implementation

Method 1: char*

char* a = "abc";

When executing the above instruction, there are 3 steps in total:

  1. Create a pointer a to char type and store it in stack segment in C memory layout.
  2. Allocate the necessary memory in DS segment for the string abc.
  3. Return the address of this memory as the value and assign to the pointer a.
    Finally, it is a char* pointer to an initialized data stored in DS segment.

In fact, if now we set char* aa = "abc", then will only execute the step 1 and 3, because the string constant abc has been created in the DS segment.

Therefore, it is can be assigned other string like below operation:

a = "def"; // ok, because a is a pointer

However, because the abc is a constant, stored in the DS segment(i.e. data segment), it can not be modified. In other words, we can access the underlying elements via subscript operations in the below example, but we cannot modify the element:

char* a = "abc";
printf("%c\n", a[0]); // ok, print 'a'
a[0] = 'A'; // error, can compile but will crash
a = "def";  // ok, because a is a pointer

Method 2: char[]

char b[3] = "abc";              // it is a string literal, has '\0' in the end as default
char b[3] = {'a','b','c'};      // it is a character array, has no '\0' in the end as default

char[] is a character array in C language. When executing the above instruction, there are only 2 steps in total:

  1. Declare an array with char type, please note in this context b is an array, if you are not clear about the difference between array and pointer, click here. This moment, the array variable b stored in the stack segment, and: &b == b == &b[0].
  2. Assign each character of 123 to the each element of array b.
    Finally, the value of this array is equal to abc, instead of a pointer to point the constant like char*. Because it is an array type, it is contrary with char*: the assignment with string constant is illegal, but the subscript operation is legal.

    b = "def"; // reassign with string constant will cause crash
    printf("%c\n", b[0]); //legal
    

    array can be viewed as a char * const pointer; therefore, we cannot reassign the string literal or other array to b. Otherwise, you will get an error: cannot modify lvalue.

Array decay

In terms of a special situation, like using they as the function parameters. Specifically, if an expression of type char[] is provided where one of type char* is expected, the compiler automatically converts the array into a pointer to its first element.

For example, in the function printSomething expects a pointer is passed, but if you try to pass an array to it like this:

char s[10] = "hello";
printSomething(s);

Then the compiler pretends that your code like this:

char s[10] = "hello";
printSomething(&s[0]);

Dynamic memory

char* s = (char *)malloc(10*sizeof(char));
s = "abc"; // legal
s = "def"; // legal
printf("%c\n", s[0]); // legal

This method defines a pointer that point an allocated memory. Therefore, it is legal for s to be reassigned by the constant string like def. Also, it can do the subscript operation.

References


转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 gzrjzcx@qq.com

文章标题:char* VS char[]

文章字数:637

本文作者:Alex Zou

发布时间:2019-02-22, 22:59:41

最后更新:2024-07-10, 03:02:36

原始链接:https://www.hellscript.cc/2019/02/22/subposts_c/charPointer-VS-charArray/

版权声明: "署名-非商用-相同方式共享 4.0" 转载请保留原文链接及作者。

目录
×

有钱的捧个钱场,没钱的借钱也捧个钱场