Char * P = "ABC" and char P [] = "ABC"

Source: Internet
Author: User

This article comes from the network source: Click me

 

There is such a piece of code:

#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, no output in Linux)
Get_string_2: Hello world!

 

1. Why is the output result like this?

2. StringIs "ABC" a constant?

3. char * P = "ABC"; is it invalid to write the data to the bottom?

 

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! "; // Declares an array P, which is 12 bytes in size.
Char * P = "Hello world! "; // Declares a pointer P pointing to" Hello world !" Start position of the string.

From the storage location:
Char P [] = "Hello world! "; // The P array is stored in the stack as a local variable;
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 );

The first question: Why is the output result like this?

After the C function is executed, it clears the stack and does not exist in the static data zone or heap. Therefore, after the get_string_1 () function is executed, the stack memory is released, so there is no "Hello World!" at all! "The declared memory cannot be output.

 

The second question: "ABC" is a constant? Answer: sometimes it is, sometimes it is not
(1) Not a constant:

"ABC" is not used as the initial value of the character array, as shown in figure
Char STR [] = "ABC ";
Because it defines a character array, it is equivalent Space defined(Note !)To store "ABC", and because the character array stores the characters one by one, the compiler resolves this statement
Char STR [3] = {'A', 'B', 'C '};
If it appears as a string, the compiler will automatically add a 0 to the string as the Terminator, so the final result of char STR [] = "ABC"; is
Char STR [4] = {'A', 'B', 'C', '\ 0 '};
If char STR [] = "ABC"; is written in the function, the "ABC \ 0" here should be placed on the stack because it is not a constant. (2) constants:
When "ABC" is assigned to a character pointer variable, such
Char * PTR = "ABC ";
Because it defines a common pointer, No space defined (note !)To store "ABC", so the compiler has to help us find a place to put "ABC". Obviously, taking "ABC" as a constant and placing it in the constant area of the program is the most appropriate choice for the compiler. Therefore, although the PTR type is not const char * And PTR [0] = 'X'; can also be compiled, run PTR [0] = 'X '; an exception occurs during running, because this statement attempts to modify things in the constant area of the program. Third, char * P = "Hello world! "; Is it invalid to write to the bottom?I remember which book once said char * PTR = "ABC"; this writing method was originally not allowed in the C ++ standard, but it is too much in C, to be compatible with C, it is not allowed. Although allowed, we recommend that you write const char * PTR = "ABC". In this way, if PTR [0] = 'X' is followed, the compiler will not let it compile, this avoids the running exception mentioned above. Expand it.: 1. if char * PTR = "ABC"; is written in the function body, although "ABC \ 0" is put in the constant area, PTR itself is only a common pointer variable, therefore, PTR is put on the stack, but the things it points to are put in the constant zone.
2. the type of the String constant can be understood as the type of the corresponding character constant array. For example, the type of "abcdef" can be considered as const char [7].
3. If you really need to use "ABCD" as the pointer, it is recommended to write it as const char * P = "ABCD ";
4. If the string array is initialized, it is recommended to write it as char P [] = "ABCD ";
5. If P is a pointer and needs to be initialized, it should be char * P; P = malloc (str_size); strcpy (P, "ABCD ");

Char * P = "ABC" and char P [] = "ABC"

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.