The difference between Char str[] and char *str detailed parsing _c language

Source: Internet
Author: User
Tags constant

Copy Code code as follows:

char* get_str (void)
{
Char str[] = {"ABCD"};
return str;
}

Char str[] = {"ABCD"}; defines a local character array, although it is an array, but it is a local variable, and returning its address must be the address of a space that has been freed.

This function returns the address of the internal local character array str, and the array is destroyed after the function is called, so the pointer you return points to a destroyed memory, which is wrong.

Copy Code code as follows:

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

char* str = {"ABCD"}; indicates that a string constant is first defined and its address is assigned to STR.
This function returns the address of a string constant. And like this string is the global, in the compile time has been allocated memory, only when the program exits will be destroyed, so the return of its address is no problem, but you'd better return the constant pointer, because you can not change the value of the string constant.
Copy Code code as follows:

const char str[] = "ABCD"; ABC is stored on the stack
const char *STR = "ABCD"; ABC is stored in a static storage area

To be exact, the top two "abc" are stored in the static storage area, the constant area. The constant area is readable and not writable. So any attempt to write to a constant area is illegal, of course, this is not necessarily not writable, you can take some channel to change the memory properties of the constant area, such as changing the properties of the PE-related section can read and write to the constant area, of course, this can be ignored at present ...

So why str[] = "ABC";
Can you write it?
The answer is str[] = "ABC"; there is an additional copy process that copies the "ABC" of the constant area to the stack memory, so it can be written.

Summarize:
All characters and strings that are contained in "or" are constants and should be stored on the heap.

Copy Code code as follows:

Char *str = "xxxxx", str points to the constant address.
Char str[] = "xxxxx", str application space on the stack, copy the constant content in, so it is a local variable.

First, the array and the pointer are different data types, and there are essential differences:
Copy Code code as follows:

Char str[] = "ABCD"; sizeof (str) = = 5 * sizeof (char)
char * str = "ABCD"; sizeof (str) = = 4 (x86) or 8 (x64)

Arrays can be automatically transformed into pointers, and pointers cannot be transferred to a group.

The string is then equivalent to a character array, not to a character pointer. According to the previous article, a string can be automatically transformed into a character pointer.

Then again, "ABCD" is called "string constant," and any type of constant is a right (a temporary variable without a name), and the "ABCD" must be left (with a name variable) to be able to modify the "ABCD" string.

Copy Code code as follows:

Char str[] = "ABCD"; The equals sign ends with the same data type, and the right value becomes the left value
char * str = "ABCD"; The equal ends are different data types, the right end is automatically transformed into char*, the char* gets the name of STR, and the "ABCD" char array still has no name.

char * str is stored in the global static store, so, although it is a local variable, it can still get the correct value after the function returns!
Char str[] is stored on the stack, the local variable, the function returned, the OS to reclaim space, it ceased to exist, so, get the right result!

Char str[]= "Name", and unlike Char str[5];str= "name", can you tell from the point of view of memory allocation, I know that the array name is a constant address (pointer), the first why is the second, what is wrong?

The second defines an array, knowing that the array name STR is the first address of the space allocated to the array, and that str= "name" should be an error that does not match the type of the equals sign. A general constant should not have a memory address unless a variable points to the constant.

The array name is an address constant, so constants are certainly not allowed to be assigned a value.
' Name ' is a string constant he is stored in a constant store and can only point to it with a pointer but it is not allowed to change: char*p;p= "name";
In general, Char str[]= "name"; the space on the stack is allocated by the compiler, 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.