C + + Output char-type variable and string address

Source: Internet
Author: User

in C If the pointer is a basic numeric type
int, Foalt, long, etc., if the output has
cout << ptr << Endl;
Where PTR is the name of the pointer, the output value is the address of the pointer,
If it is a char * (char pointer), the value of the char pointer is output.

c++ int and char char The address of the variable? Give the following example:

int i=97;

Char c= ' a ';

cout<< "line 1:i=" <<i<< "\t\t" << "c=" <<c;

cout<< "\nline 2: &i=" <<&i<< "\ T" << "&c=" <<&c;

I think, for Line 1 the output is no doubt, and, I think for the beginner pointer friend, you may also like me, do not hesitate to think that the output will be two addresses, but, you only answer half!

Indeed,Line 2in the&ithe output isintvariablesIaddress, but when you see the results of the run, you should not be surprised: why&cthe output is a garbled?

first of all, I would like to first introduce the C + + the basics of string in.

c++ " 3

Char *str= "string";

Char str2[]= "string2";

Char str3[]={' s ', ' t ', ' r ', ' I ', ' n ', ' G ', ' 3 ', ' + '};

cout<< "line 3:str=" <<str<<endl;

cout<< "line 4:str2=" <<str2<<endl;

cout<< "line 5:str3=" <<str3<<endl;

&NBSP;

run, this 3 The output of the row is the value of the saved string, not the address we think it is. Then we can contact the front &c In fact, this is a char * &c The saved string is no terminator, So the output is garbled.

recently, in reading theC + +Programming Tutorial (Section4version) Section AChapter, I solved this doubt. In fact, any programmer wishing to output a pointer variable for an address would have to make a conversion, that is, forcingchar *converted intovoid *, then,CharThe address of the type variable and the string can be output in the format of the 16 mechanism, as follows:

cout<< "line 6:static_cast<void *> (&c) =" <<static_cast<void *> (&c) <<endl;

cout<< "line 7:static_cast<void *> (str) =" <<static_cast<void *> (str) <<endl;

cout<<  (int *) str<<endl; can also

at this point, you can see that the result of the output is Char The address of the type variable and the string variable.


C + + Output char-type variable and string address

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.