Last Friday to participate in Realtek's pen test, there is a choice question, presumably this:
const char a1[] = "abc"; const char a2[] = "abc"; const char *P1 = "abc"; const char *P2 = "abc";
A, A1 and A2, P1 and P2 are not the same;
B, A1 and A2 not the same, p1 and p2;
C, A1 and A2 are different, P1 and P2 are not the same;
D, A1 and A2, like P1 and p2;
In the VS2005 environment, I knocked the code into the simulation and found that:
a1=0x004156b0;
A2=0X004156B4;
p1=0x0041563c;
p2=0x0041563c;
Thus, the correct answer should be B.
So why choose b for this question? Later asked, the conclusion is this:
For example: const char a1[] = "abc"; A step like this:
Of course the actual code in this split to write, it should be wrong, here just to illustrate the process
1, const char a1[];//this time the system allocates a space, the first address of the space is the array name A1
2, a[1] = "abc";//Load the string into the allocated memory unit
Thus it seems that the first two are allocated space, as for the space inside what with the allocation of the first address of the space is irrelevant, that is, the compiler will allocate two first address different space to two arrays, so the first address of the two array is of course different, so A1 and A2 are different.
The latter two, for example: const char *P1 = "ABC"; it is a process like this:
Of course the actual code in this split to write, it should be wrong, here just to illustrate the process
1, const char *P1;
2, p1 = "abc";
In other words, we first define a char* pointer and then assign "ABC" to the pointer. So what is "abc" here? is obviously a constant string, and the value of the constant string is actually an address that points to the string. In other words, "abc" is actually an address, and since the string is a constant string, the pointer is a constant.
So whether p1= "abc", p2 = "abc" or p3 = "abc", this pointer constant is assigned to P1,P2 and P3, so p1=p2;
Today, when searching for related questions on the Internet, I see people asking the following questions:
int isequiv (char *t) {if (t = = "char") return 1;elsereturn 0;} int main () {char test[] = "char", char *temp = "char";p rintf ("%d", isequiv (Test)),//Output 0printf ("%d", isequiv (temp));//Output 1g Etchar (); return 0;}
Asked the classmate to ask why the two printf output is not the same, in his opinion should be the same. According to my explanation above, the output is obviously not the same.
At last, I saw a small knowledge point on the net, which is a question about the string constant pointer, the original text is as follows:
Using pointers to store strings, the essence of which is to attach the first value of the string to a pointer variable of the base type char, so that the string can be manipulated from the beginning of the string, and there is a problem.
Use this subclass to explain . int main () &NBS P { char *p= "Hello World"; p[ 0]= ' H '; printf ("%s\n", p); return 0;  ; } Run results will be broken because *p= "Hello World" simply declares a pointer variable pointing to the string "Hello World" and " Hello World "This string program does not allocate space to it, and the compiler assigns it to the constant area. The value of the constant string is not allowed to be modified, so a break error occurs . The program changes to the following is correct int main () { char p[12]= "Hello World"; char *p1=p; p1[0]= ' H '; printf ("%s\n", p1); return 0; &nbs P } &NBsp The reason is that p[12]= "Hello World" is your own definition of a character array of length 12, so the string "Hello World" compiler will assign it space (in the stack), so you can modify its value. finally I see at the end of the p153 page C and pointers: char message1[] = "Hello"; char *message2 = "Hello"; the two initializations look alike, but they have different meanings. The former is an element that initializes a character array, while the latter is atrue String Constants。this pointer variable is initialized to the storage location that points to the string constant.
A Realtek C-pen question