Difference between char STR [] and char * Str

Source: Internet
Author: User
char* get_str(void){char str[] = {"abcd"};return str;}

Char STR [] = {"ABCD"}; defines an array of local characters. Although it is an array, it is a local variable, the returned address must be the address of the space that has been released.
This function returns the internal STR address of a local character array, and the array is destroyed after the function is called. Therefore, the pointer you return also points to a piece of memory that is destroyed, this statement is incorrect.

char* get_str(void){char *str = {"abcd"};return str;}

Char * STR = {"ABCD"}; defines a String constant and assigns its address to Str.
The return value of this function is the address of the String constant, and a string like this is global. The memory has been allocated during compilation and will be destroyed only when the program exits, so there is no problem in returning its address, but you 'd better return a constant pointer, because you cannot change the value of a String constant.

This STR is in the stack, but the strings behind it are in the constant zone. When the function returns the address of the constant zone, the scope ends, and the space of STR in the stack is released ..

Const char STR [] = "ABCD"; // ABC is stored in the stack. Const char * STR = "ABCD"; // ABC is stored in the static storage zone.

To be accurate, the above two "ABC" are both stored in the static storage area, that is, the constant area. The constant area is readable and cannot be written. Therefore, any attempt to write a constant partition is illegal. Of course, this is not necessarily not writable. You can change the Memory attribute of the constant partition through some channel, for example, you can change the attributes of the PE joint to read and write the constant zone. Of course, this can be ignored at present...
So why can STR [] = "ABC"; be written? The answer lies in STR [] = "ABC"; there is an additional copy process, that is, copying the "ABC" of the constant area to the stack memory, so you can write it.


Summary:
All characters and strings contained in "" or ''are constants and should be stored on the stack.
Char * STR = "XXXXX", STR points to the constant address.
Char STR [] = "XXXXX", STR applies for space on the stack, copies the constant content, so it is a local variable.

First, arrays and pointers are different data types, essentially different:
Char STR [] = "ABCD"; // sizeof (STR) = 5 * sizeof (char)
Char * STR = "ABCD"; // sizeof (STR) = 4 (x86) or 8 (x64)
Arrays can be automatically converted to pointers, but pointers cannot be converted to arrays.

Then, a string is equivalent to a character array, not a character pointer. Based on the previous one, the string can be automatically converted into a character pointer.

Then, "ABCD" is called "String constant", and any type of constant is the right value (a temporary variable without a name ), you must make "ABCD" A left value (a variable with a name) to modify the "ABCD" string.
Char STR [] = "ABCD"; // the two ends of the equal sign are the same data type, and the right value is the left value.
Char * STR = "ABCD"; // the two ends of the equal sign are different data types, and the right side is automatically converted to char *. The char * obtains the STR name, the char array "ABCD" still has no name.

Char * STR is stored in the Global static storage area. Therefore, although it is a local variable, you can still get the correct value after the function returns it!
Char STR [] is stored on the stack. Local variable. After the function returns, the OS will reclaim the space and no longer exist. Therefore, the correct result cannot be obtained!

Char STR [] = "name"; the difference between char STR [5]; STR = "name" and char STR [5]; can you tell us about memory allocation, I know that the array name is a constant address (pointer). The first is correct, and the second is wrong?

The second one first defines an array. You need to know that the array name STR is the first address of the space allocated to the array. Str = "name" should be an error of mismatched types on both sides of the equal sign. A normal constant should have no memory address unless a variable points to the constant.
If the array name is an address constant, the constant cannot be re-assigned.
"Name" is a String constant stored in the constant storage area. It can be pointed to by only one pointer but cannot be changed: char * P; P = "name ";
In general, char STR [] = "name"; the space in the array is allocated by the compiler on the stack, and the content can be changed by the user.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.