Pointer is equal to string?

Source: Internet
Author: User

Chatting with a friend a few days ago, he is not familiar with char * s = "hello"; this writing method, how to assign a string to a pointer? What does it mean? In fact, we can understand this question in detail: First of all, this sentenceCodeIt exists in a function or is global. If it exists in a function, such:

Void somefunc ()
{
Char * s = "hello ";
MessageBox (0, S, S, mb_ OK );
}

SoProgramAfter entering this function, "hello" is allocated as a String constant in the stack space of this function, occupying 6 bytes of space, and at the first address of this string constant, will be used to initialize S. After the function is executed, the stack space of the function will be released (The String constant "hello" and pointer s are both in the stack space of the function, so they will be released ), do not worry about memory leakage.

If char * s = "hello"; is not in any function, the code is global, the variable s can be accessed throughout the entire life cycle of the program and in any scope. At this time, "hello" is a String constant in the global Constant Area of the program. At the beginning of the program running, "Hello" constants and S are both created in the global constant area and variable area, and S is automatically initialized with the first address of "hello. When the program exits, it is automatically released.

Similar to this Code, there are many writing methods. For example, you can write MessageBox (0, "hello", "hello", mb_ OK) when calling an API function );
This is the same principle that the string is passed to the char * parameter.

Related Article

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.