Because there is an articleArticleI did not write much since I wrote it clearly. I posted it here for ease of viewing.
Take a look at the followingProgramOutput:
# Include <stdio. h>
Char * returnstr ()
{
Char * P = "Hello world! ";
Return P;
}
Int main ()
{
Char * STR;
STR = returnstr ();
Printf ("% s/n", STR );
Return 0;
}
There is no problem with this, because "Hello world! "Is a String constant, which is stored in the static data zone. The first address of the static data zone where the String constant is stored is assigned to the pointer. Therefore, when the returnstr function exits, the memory where the String constant is located will not be recycled, so it can be accessed smoothly through the pointer.
However, the following issues occur:
# Include <stdio. h>
Char * returnstr ()
{
Char P [] = "Hello world! ";
Return P;
}
Int main ()
{
Char * STR;
STR = returnstr ();
Printf ("% s/n", STR );
Return 0;
}
"Hello
World! "It is a String constant that is stored in the static data zone. That's right, but a String constant is assigned to a local variable (char
[] Type array), the local variable is stored in the stack, so there are two pieces of memory with the same content. This is the most essential difference with the previous one. When the returnstr function exits, stack needs to be cleared, local variable memory
It is also cleared, so the function returns a released memory address, so the output is garbled.
If the return value of a function is not the address of a local variable, the local variable must be declared as static. As follows:
# Include <stdio. h>
Char * returnstr ()
{
Static char P [] = "Hello world! ";
Return P;
}
Int main ()
{
Char * STR;
STR = returnstr ();
Printf ("% s/n", STR );
Return 0;
}
This problem can be better explained through the following example:
# Include <stdio. h>
// The returned address is the address of the local variable, which is located in the dynamic data zone and in the stack.
Char * S1 ()
{
Char P [] = "Hello world! ";
Printf ("in S1 P = % P/N", P );
Printf ("in S1: String's address: % P/N", & ("Hello world! "));
Return P;
}
// Return the address of the String constant, which is located in the static data zone.
Char * S2 ()
{
Char * q = "Hello world! ";
Printf ("in S2 q = % P/N", q );
Printf ("in S2: String's address: % P/N", & ("Hello world! "));
Return Q;
}
// The address of the static local variable is returned. The address is located in the static data zone.
Char * S3 ()
{
Static char R [] = "Hello world! ";
Printf ("in S3 r = % P/N", R );
Printf ("in S3: String's address: % P/N", & ("Hello world! "));
Return R;
}
Int main ()
{
Char * T1, * t2, * T3;
T1 = S1 ();
T2 = S2 ();
T3 = S3 ();
Printf ("in main :");
Printf ("P = % p, q = % P, R = % P/N", T1, T2, T3 );
Printf ("% s/n", T1 );
Printf ("% s/n", T2 );
Printf ("% s/n", T3 );
Return 0;
}
Output result:
In S1 P = 0xbff92efb
In S1: String's address: 0x80486ac
In S2 q = 0x80486ac
In S2: String's address: 0x80486ac
In S3 r = 0x804998c
In S3: String
This result proves the above explanation. At the same time, we can draw a conclusion:
A String constant is called a constant because it can be regarded as an unnamed string and is a constant and stored in the static data zone. The static data zone mentioned here is relative to the dynamic data zone such as heap and stack.
. The static data area stores global and static variables. In this regard, string constants can also be called an unknown static variable because "Hello
World! "This string is referenced in functions S1 and S2, but there is only one copy in the memory, which is similar to static variables.
Char * P = "abcdefg"; // static storage area
Char P [] = "abcdefg"; // P itself is the array name. the string in the array is a local variable and the content is a copy of the original static area content! Therefore, the local variable address actually returned by P instead of the static storage area address is different from the above!