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.