Parse char * P and char P []

Source: Internet
Author: User

Reprinted. char * P = "Hello world! "; Content in" "is in the static Zone

But some people say it is in the constant area.

The memory occupied by a C/C ++ compiled program is divided into the following parts:
1. STACK: the stack zone is automatically allocated and released by the compiler, and stores function parameter values and local variable values. Its
The operation method is similar to the stack in the data structure.
2. Heap-generally assigned and released by the programmer. If the programmer does not release the heap, it may be recycled by the OS at the end of the program. Note that it is different from the heap in the data structure. The allocation method is similar to the linked list.
3. Global (static)-the storage of global variables and static variables is put together, and the initialized global variables and static variables are in one area, uninitialized global variables and uninitialized static variables are in another adjacent area. The program is released by the system.
4. Text Constant Area-constant strings are placed here. The program is released by the system.
5. program code area-stores the binary code of the function body.

--------------------------------

Test result (vs2008)

P [2] = 'D'; // The compilation is normal, but the running crashes.

Some people say that "" content is in the constant area and cannot be modified. It cannot be modified in the above method.

But (* P) ++; then we will find that if * P is output, it is iello world !;

 

Here, by the way, * P ++;

++ Has a higher priority than *. P ++ is executed first, but this ++ operation is written later. Therefore, P is extracted first, * P is performed, and then ++ is performed;

The parameters in printf are calculated from the right to the left. Therefore, first calculate (* P) ++, that is, return the value of a [0], and then auto-increment a [0; then calculate * P ++. This expression calculates P ++ first, returns the current P value, and then increases P. Therefore, this is equal to the value of the current A [0; the value of a [1] For * P is obtained.

 

Main issues resolved:

Question 1:
# Include "stdio. H"
Char * get_string_1 ()
{
Char P [] = "Hello world! ";
Return P;
}
Char * get_string_2 ()
{
Char * P = "Hello world! ";
Return P;
}
Int main ()
{
Char * P;
P = get_string_1 ();
Printf ("get_string_1: % s \ n", P );
P = get_string_2 ();
Printf ("get_string_2: % s \ n", P );
Return 0;
}
Output:
Get_string_1: (garbled or no output). There is no output in Linux.
Get_string_2: Hello world!
Why?

Question 2:
Char * P = "Hello world! "; It is invalid to write to the bottom, so it cannot be written in practice.

Question 3:
Char * P = "Hello world! "; With Char P [] =" Hello world! "; What are the differences between details and operations.

Resolution:
Char * P = "Hello world! "; With Char P [] =" Hello world! "; Both are used to declare a string and initialize it as Hello world !, However, the meaning is indeed quite different.
From the declared object:
Char P [] = "Hello world! "; Is used to declare an array P. The array size is 12 bytes.
Char * P = "Hello world! "; To declare a pointer P pointing to" Hello world !" Start position of the string.
From the storage location:
Char P [] = "Hello world! "; P arrays are stored in the stack as local variables;
Char * P = "Hello world! "; In this statement," Hello world! "It is stored in the static data area and is global. P is just a pointer and points to this area. If you don't believe it, try the following code to see if it is the same address:
Char * P1 = "Hello world! ";
Char * P2 = "Hello world! ";
Printf ("P1: % x \ NP2: % x \ n", P1, P2 );
From the perspective of the scanning work after function execution:
After the C function is executed, it clears the stack and does not exist in the static data zone or heap. Therefore, it is not difficult to explain the first problem. get_string_1 () after the function is executed, the stack memory is released, so there is no "Hello World! "The declared memory cannot be output.

Then, write it as char * P = "Hello world! "; Is the combination illegal? Can I write it like this?
This is a historical issue. Before the const keyword is introduced into the C language, this write method is legal and has been used for a long period of time. A large amount of Code uses this method during this period, the new C language is allowed to be written in this way for compatibility purposes, but it is best not to, because this writing method will eventually be eliminated. Maybe your code will use the new version of the compiler someday, then an inexplicable problem occurs. It is not easy to find this bug. It is best to write it as follows:
Const char * P = "Hello world! ";
Or
Char P [] = "Hello world! ";
What is the difference between operations and details?
There are many differences. Due to my lack of learning skills, I only summarized the following points and forgot to add them:
1. char * P = "Hello world! "; You can use the P ++ operation, sizeof (p) = 4;
2. Char P [] = "Hello world! "; Sizeof (p) = 12;

Reprinted with address: http://yuhuafx.blog.hexun.com/35683001_d.html

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.