Char STR [] = {"ABCD"} and char * STR = {"ABCD "}

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 the nominal value of a string like this is global. The memory has been allocated during compilation, and onlyProgramIt will be destroyed only when exiting, so it is okay to return its address, but you 'd better return a constant pointer because you cannot change the value of a String constant.

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.