(go) parse char *p and char[]

Source: Internet
Author: User

See the Forum on the discussion of this issue, the feeling is also involved in a relatively broad, and many people have made this mistake, such as declaring char *p = "Hello world!"; I tried to change the string content with the P pointer, so I summed up the question. The reprint indicates the author Logic0 and the source. Main analytic issues: Issue 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), Linux does not have any output
Get_string_2:hello world!
Why is that? Question 2:
char *p = "Hello world!"; It is not possible to write this in the actual application. Question 3:
char *p = "Hello world!"; with char p[]= "Hello world!"; What is the difference between the details and the operation. Analytical:
char *p = "Hello world!"; With char p[] = "Hello world!"; Both are used to declare a string and initialize it to Hello world!, but the meaning of the expression is very different.
From the object it declares:
Char p[] = "Hello world!"; Used to declare an array p with an array size of 12 bytes.
char *p = "Hello world!"; Used to declare a pointer p, pointing to "Hello world!" The starting position of the string.
From the storage location:
Char p[] = "Hello world!"; The p array is stored as a local variable in the stack area;
char *p = "Hello world!"; In this statement, "Hello world!" is stored in the static data area, and is global, p is just a pointer to this area. If you don't believe it, you can try the code below to see if it's the same address:
char *p1 = "Hello world!";
char *p2 = "Hello world!";
printf ("p1:%x\np2:%x\n", P1,P2);
From the completion of the function after the work to see:
c function after the execution of the stack area cleanup operations, the static data area and the heap is not, so the first problem is not difficult to explain, the get_string_1 () function after the release of the stack memory, so there is no "Hello world!"        When declaring the memory, it is impossible to output. Well, write char *p = "Hello world!"; How does it fit? Can you write like this?
This is a historical issue, before the Const keyword was introduced in C, it is legal, and there is a long period of time, a lot of code in this period, the use of this notation, the new version of C language for compatibility, it is allowed to write, but it is best not to do so, because this writing will eventually be eliminated, Perhaps one day your code with a new version of the compiler, and then inexplicable problem, to find this bug is not an easy thing to estimate. Now it's best to write:
const char *p = "Hello world!";
Or
Char p[] = "Hello world!";
What difference do they have in the operation and the details?
A lot of difference, because I caishuxueqian, only summed up the following points, forget to add:
1. Char *p = "Hello world!"; Can be operated with p++, sizeof (p) = = 4;
2.char p[]= "Hello world!"; sizeof (P) = = 12; See the Forum on the discussion of this issue, the feeling is also involved in a relatively broad, and many people have made this mistake, such as declaring char *p = "Hello world!"; I tried to change the string content with the P pointer, so I summed up the question. The reprint indicates the author Logic0 and the source. Main analytic issues: Issue 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), Linux does not have any output
Get_string_2:hello world!
Why is that? Question 2:
char *p = "Hello world!"; It is not possible to write this in the actual application. Question 3:
char *p = "Hello world!"; with char p[]= "Hello world!"; What is the difference between the details and the operation. Analytical:
char *p = "Hello world!"; With char p[] = "Hello world!"; Both are used to declare a string and initialize it to Hello world!, but the meaning of the expression is very different.
From the object it declares:
Char p[] = "Hello world!"; Used to declare an array p with an array size of 12 bytes.
char *p = "Hello world!"; Used to declare a pointer p, pointing to "Hello world!" The starting position of the string.
From the storage location:
Char p[] = "Hello world!"; The p array is stored as a local variable in the stack area;
char *p = "Hello world!"; In this statement, "Hello world!" is stored in the static data area, and is global, p is just a pointer to this area. If you don't believe it, you can try the code below to see if it's the same address:
char *p1 = "Hello world!";
char *p2 = "Hello world!";
printf ("p1:%x\np2:%x\n", P1,P2);
From the completion of the function after the work to see:
c function after the execution of the stack area cleanup operations, the static data area and the heap is not, so the first problem is not difficult to explain, the get_string_1 () function after the release of the stack memory, so there is no "Hello world!"        When declaring the memory, it is impossible to output. Well, write char *p = "Hello world!"; How does it fit? Can you write like this?
This is a historical issue, before the Const keyword was introduced in C, it is legal, and there is a long period of time, a lot of code in this period, the use of this notation, the new version of C language for compatibility, it is allowed to write, but it is best not to do so, because this writing will eventually be eliminated, Perhaps one day your code with a new version of the compiler, and then inexplicable problem, to find this bug is not an easy thing to estimate. Now it's best to write:
const char *p = "Hello world!";
Or
Char p[] = "Hello world!";
What difference do they have in the operation and the details?
A lot of difference, because I caishuxueqian, only summed up the following points, forget to add:
1. Char *p = "Hello world!"; Can be operated with p++, sizeof (p) = = 4;
2.char p[]= "Hello world!"; sizeof (P) = = 12; See the Forum on the discussion of this issue, the feeling is also involved in a relatively broad, and many people have made this mistake, such as declaring char *p = "Hello world!"; I tried to change the string content with the P pointer, so I summed up the question. The reprint indicates the author Logic0 and the source. Main analytic issues: Issue 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), Linux does not have any output
Get_string_2:hello world!
Why is that? Question 2:
char *p = "Hello world!"; It is not possible to write this in the actual application. Question 3:
char *p = "Hello world!"; with char p[]= "Hello world!"; What is the difference between the details and the operation. Analytical:
char *p = "Hello world!"; With char p[] = "Hello world!"; Both are used to declare a string and initialize it to Hello world!, but the meaning of the expression is very different.
From the object it declares:
Char p[] = "Hello world!"; Used to declare an array p with an array size of 12 bytes.
char *p = "Hello world!"; Used to declare a pointer p, pointing to "Hello world!" The starting position of the string.
From the storage location:
Char p[] = "Hello world!"; The p array is stored as a local variable in the stack area;
char *p = "Hello world!"; In this statement, "Hello world!" is stored in the static data area, and is global, p is just a pointer to this area. If you don't believe it, you can try the code below to see if it's the same address:
char *p1 = "Hello world!";
char *p2 = "Hello world!";
printf ("p1:%x\np2:%x\n", P1,P2);
From the completion of the function after the work to see:
c function after the execution of the stack area cleanup operations, the static data area and the heap is not, so the first problem is not difficult to explain, the get_string_1 () function after the release of the stack memory, so there is no "Hello world!"        When declaring the memory, it is impossible to output. Well, write char *p = "Hello world!"; How does it fit? Can you write like this?
This is a historical issue, before the Const keyword was introduced in C, it is legal, and there is a long period of time, a lot of code in this period, the use of this notation, the new version of C language for compatibility, it is allowed to write, but it is best not to do so, because this writing will eventually be eliminated, Perhaps one day your code with a new version of the compiler, and then inexplicable problem, to find this bug is not an easy thing to estimate. Now it's best to write:
const char *p = "Hello world!";
Or
Char p[] = "Hello world!";
What difference do they have in the operation and the details?
A lot of difference, because I caishuxueqian, only summed up the following points, forget to add:
1. Char *p = "Hello world!"; Can be operated with p++, sizeof (p) = = 4;
2.char p[]= "Hello world!"; sizeof (P) = = 12;

(go) parse char *p and char[]

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.