Initialize the pointer string constant

Today, write a small text talk address string and string constants.

In C / C ++ in a string constant represents the address of the first element of the string, just like the name of the pointer char, char array name indicates the address of the first element as a string.

Want to print an address, with a simple cout << address; you can get the statement;

However, the following two statements that will print the entire string

char a[20] = "1234";
cout << a << endl;

char *p = a;
cout << p << endl;

This is also a character array and an array of other different places, then the address of the string how to get it?

Here are two ways to reference

cout << (int*)a << endl;
cout << &a << endl;

Both methods can be printed out correctly "address of the string," but there are subtle differences place

In a character array, a represents the first address of a character, a + 1 represents the address of the second character;

In the first statement of the print address, (int *) a only acts as a cast, in other words, a represents the address of the first character, but cout << a; output the entire string is this is because the address is of type char *, cout recognition to type char * address will be printed automatically from the beginning of the address space of the points until it encounters '\ 0' string contents , so here we only need a casts (here, for example cast int *, can be converted to other types of indicators, if not char *, double * may be even print the correct address). So if you want to address the second character of it, cout << (int *) ( a + 1); can be friends

The second print statement addresses, the direct use & a; It should be emphasized that the addresses are the first element of the array regardless of any array name indicated, & is the address of the array array name indicates, though both print out the same address, but if the pointer arithmetic difference to other days , and look at the code following effects

int a[10];
cout << "a  is: " << a << " a+1  is: " << a+1 << endl;
cout << "&a is: " << &a << " &a+1  is: " << &a+1 << endl; 

Map with the truth, a + 1 backward movement only four bytes, and & a + 1 directly moved backward by 40 bytes, the address of the entire array is the array name & representation. So by the same token & character array name you can also print out the address of the string.

 The following talk string constants, static storage area in the first, second, string storage can not be modified.

And thus a few sentences below

char a[20] = "1234";
char * p = "1234";
const char * p = "1234";

The first statement Needless to say, very common, and correct, which twenty-three statement to which it wrong.

At first I thought plus without const does not matter, anyway, is wrong, then think very fine fear, really is poor knowledge limits the imagination;

Beginning I think the reason behind the two is wrong because not seen, looking like a * p assigned to a "1234", and can not, this is what, in short, is looking at the strange;

So here it involves a knowledge of the point mentioned at the beginning; in C / C ++ in a string constant represents the address of the first element of the string, just like the name char pointer, char array name indicates the character the same address of the first element of the string

So this time will understand, saw that it was "1234", in fact, is essentially a "1234" address of the first character, then type char * pointer p pointing to "1234" there is no problem anymore address to address Well, but after compiling a mistake, on the grounds that "1234" is a constant, so adding the const-qualified.

Here we must add about why const limited only correct, if not, then the type of match is, why it will complain, so there need to re-emphasize, "1234" is a string constant, constant can not be modified , if the char * p = "1234"; compiled successfully, it means that "1234" may be modified, but this is not allowed, it must obey plus the const qualifier to make the system feel at ease is the thing.

Huh? Constants can not be modified to add const qualifier can, why we have been with the first statement is correct and has never been reported to miss it?

The difference between the first and third sentence of this statement we should talk about it, "1234" is a string constant, constant stored in the static storage area, address

The third statement is to make a constant pointers directly to static memory address "1234", because it is directly to deity address, so to add const qualifier

And a copy of the first statement sucked static storage area "1234" out to replicate in a character array, so this initialization can be altered copy after "1234", this does not affect the real "1234" ;

True false address address cout will know

. 1  char a [ 20 is ] = " 1234 " ;
 2  const  char * P = " 1234 " ;
 . 3 COUT << endl << & a; // Get a character array address 
. 4 COUT & << " 1234 " << endl; // Get "1234" in the address of the static memory
 . 5  
. 6  // again to test a string constant for the address of the first element of the string 
. 7 COUT * << " 1234 " << endl;
 . 8 COUT << * ( " 1234 "+1) << endl;

 

 

OK, obviously a copy of the "1234" with the deity different from "1234" address, but also a lot worse a lot of address bits, certainly across the memory area of ​​the rearmost two rows wanted conclusion has been verified, practice makes perfect, really I live up to stay up to 12 points.

 

Guess you like

Origin www.cnblogs.com/GuoYuying/p/11391981.html