char* VS char[]
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:
- Create a pointer
ato char type and store it instacksegment in C memory layout. - Allocate the necessary memory in
DSsegment for the stringabc. - Return the address of this memory as the value and assign to the pointer
a.
Finally, it is achar*pointer to an initialized data stored inDSsegment.
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:
- Declare an array with
chartype, please note in this contextbis an array, if you are not clear about the difference betweenarrayandpointer, click here. This moment, the array variablebstored in thestacksegment, and:&b == b == &b[0]. Assign each character of
123to the each element of arrayb.
Finally, the value of this array is equal toabc, instead of a pointer to point the constant likechar*. Because it is anarraytype, it is contrary withchar*: 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]); //legalarraycan be viewed as achar * constpointer; therefore, we cannot reassign thestring literalor otherarraytob. 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" 转载请保留原文链接及作者。