C++ cout输出 字符型变量的地址
2017-02-07 15:54
211 查看
在C++中,如果cout一个字符数组的话,那么它会沿着这个地址,一直输出这个字符串,直到遇到'\0'
实际上,C++标准库中I/O类对输出操作符<<重载,在遇到字符型指针时会将其当做字符串名来处理,输出指针所指的字符串。既然这样,我们就别让他知道那是字符型指针,所以得进行类型转换,即:希望任何字符型的指针变量输出为地址的话,都要作一个转换,即强制char *转换成void *
#include<iostream>
int main(){
const short ITEMS=5;
int intArray[ITEMS]={1,2,3,4,5};
char charArray[ITEMS]={'L','M','Y','L','R'};
int *intPointer=intArray;
char *charPointer=charArray;
std::cout<<"整形数组输出"<<"\n";
for(int i=0;i<ITEMS;i++){
std::cout<<*intPointer<<" at "<<intPointer<<"\n" ;
intPointer++;
}
std::cout<<"字符型数组输出"<<"\n";
for(int i=0;i<ITEMS;i++){
std::cout<<*charPointer<<" at "<<charPointer<<"\n" ;
charPointer++;
}
return 0;
}
整形数组输出
1 at 0x29ff00
2 at 0x29ff04
3 at 0x29ff08
4 at 0x29ff0c
5 at 0x29ff10
字符型数组输出
L at LMYLR@
M at MYLR@
Y at YLR@
L at LR@
R at R@
对字符型指针进行强制类型转换之后:
#include<iostream>
int main(){
const short ITEMS=5;
int intArray[ITEMS]={1,2,3,4,5};
char charArray[ITEMS]={'L','M','Y','L','R'};
int *intPointer=intArray;
char *charPointer=charArray;
std::cout<<"整形数组输出"<<"\n";
for(int i=0;i<ITEMS;i++){
std::cout<<*intPointer<<" at "<<intPointer<<"\n" ;
intPointer++;
}
std::cout<<"字符型数组输出"<<"\n";
for(int i=0;i<ITEMS;i++){
std::cout<<*charPointer<<" at "<<(void *)charPointer<<"\n" ;
charPointer++;
}
return 0;
}
整形数组输出
1 at 0x29ff00
2 at 0x29ff04
3 at 0x29ff08
4 at 0x29ff0c
5 at 0x29ff10
字符型数组输出
L at 0x29fef0
M at 0x29fef1
Y at 0x29fef2
L at 0x29fef3
R at 0x29fef4
实际上,C++标准库中I/O类对输出操作符<<重载,在遇到字符型指针时会将其当做字符串名来处理,输出指针所指的字符串。既然这样,我们就别让他知道那是字符型指针,所以得进行类型转换,即:希望任何字符型的指针变量输出为地址的话,都要作一个转换,即强制char *转换成void *
#include<iostream>
int main(){
const short ITEMS=5;
int intArray[ITEMS]={1,2,3,4,5};
char charArray[ITEMS]={'L','M','Y','L','R'};
int *intPointer=intArray;
char *charPointer=charArray;
std::cout<<"整形数组输出"<<"\n";
for(int i=0;i<ITEMS;i++){
std::cout<<*intPointer<<" at "<<intPointer<<"\n" ;
intPointer++;
}
std::cout<<"字符型数组输出"<<"\n";
for(int i=0;i<ITEMS;i++){
std::cout<<*charPointer<<" at "<<charPointer<<"\n" ;
charPointer++;
}
return 0;
}
整形数组输出
1 at 0x29ff00
2 at 0x29ff04
3 at 0x29ff08
4 at 0x29ff0c
5 at 0x29ff10
字符型数组输出
L at LMYLR@
M at MYLR@
Y at YLR@
L at LR@
R at R@
对字符型指针进行强制类型转换之后:
#include<iostream>
int main(){
const short ITEMS=5;
int intArray[ITEMS]={1,2,3,4,5};
char charArray[ITEMS]={'L','M','Y','L','R'};
int *intPointer=intArray;
char *charPointer=charArray;
std::cout<<"整形数组输出"<<"\n";
for(int i=0;i<ITEMS;i++){
std::cout<<*intPointer<<" at "<<intPointer<<"\n" ;
intPointer++;
}
std::cout<<"字符型数组输出"<<"\n";
for(int i=0;i<ITEMS;i++){
std::cout<<*charPointer<<" at "<<(void *)charPointer<<"\n" ;
charPointer++;
}
return 0;
}
整形数组输出
1 at 0x29ff00
2 at 0x29ff04
3 at 0x29ff08
4 at 0x29ff0c
5 at 0x29ff10
字符型数组输出
L at 0x29fef0
M at 0x29fef1
Y at 0x29fef2
L at 0x29fef3
R at 0x29fef4
相关文章推荐
- C++ cout输出 字符型变量的地址
- C++中cout输出字符型指针地址值的方法
- C++中字符型变量的地址输出
- [转] C++中字符型变量的地址输出
- C++中cout输出字符型指针地址值的方法
- C++中cout输出字符型指针地址值的方法
- C++中cout输出字符型指针地址值的方法
- C++中字符型变量的地址输出
- C++中cout输出字符型指针地址值的方法
- C++中cout输出字符型指针地址值的方法 http://www.cnblogs.com/wxxweb/archive/2011/05/20/2052256.html
- C++中cout输出字符型指针地址值的方法
- C++中cout输出字符型指针地址值的方法
- C++中字符型变量的地址输出
- C++中cout输出字符型指针地址值的方法
- C++中cout输出字符型指针地址值的方法
- C++中cout输出字符型指针地址值的方法
- C++中cout输出字符型指针地址值的方法
- C++中cout输出字符型指针地址值的方法
- c++通过cout输出字符变量的地址
- 浅谈C++中char型变量的地址输出