2017-05-27 218 views
1

char *和int *有什么区别?char *和int *的区别是什么?C++ char * vs int *

例如,当我跑这样的代码:

#include<iostream> 
using namespace std; 


int main(){ 
    int a[3] = {10, 20, 30}; 
    int *ptr2a = &a[0]; 
    int i; 
    char line[] = "Hello"; 
    char *ptr2line = &line[0]; 

    for(i=0; i<3; i++){ 
    cout<<"Value of a["<<i<<"] is: "<<a[i]<<" same as "<<*(ptr2a+i); 
    cout<<" Address is: "<<ptr2a+i<<" , same as "<<&a[i]<<endl; 
    } 

    for(i=0; i<strlen(line); i++){ 
    cout<<"Value of line["<<i<<"] is: "<<line[i]<<" same as "<<*(ptr2line+i); 
    cout<<" Address is: "<<ptr2line+i<<" , same as "<<&line[i]<<endl; 
    } 

return 0; 
} 

我得到以下输出:

Value of a[0] is: 10 same as 10 Address is: 0x7fff5bf29b1c , same as 0x7fff5bf29b1c 
Value of a[1] is: 20 same as 20 Address is: 0x7fff5bf29b20 , same as 0x7fff5bf29b20 
Value of a[2] is: 30 same as 30 Address is: 0x7fff5bf29b24 , same as 0x7fff5bf29b24 
Value of line[0] is: H same as H Address is: Hello , same as Hello 
Value of line[1] is: e same as e Address is: ello , same as ello 
Value of line[2] is: l same as l Address is: llo , same as llo 
Value of line[3] is: l same as l Address is: lo , same as lo 
Value of line[4] is: o same as o Address is: o , same as o 

当我改变这一行:

cout<<" Address is: "<<ptr2line+i<<" , same as "<<&line[i]<<endl; 

到:

cout<<" Address is: "<<&ptr2line+i<<" , same as "<<&line[i]<<endl; 

我得到这样的输出:

Value of line[0] is: H same as H Address is: 0x7fff5054bad0 , same as Hello 
Value of line[1] is: e same as e Address is: 0x7fff5054bad8 , same as ello 
Value of line[2] is: l same as l Address is: 0x7fff5054bae0 , same as llo 
Value of line[3] is: l same as l Address is: 0x7fff5054bae8 , same as lo 
Value of line[4] is: o same as o Address is: 0x7fff5054baf0 , same as o 

在这种情况下 地址0x7fff5054bad0相当于“你好” 0x7fff5054bad8对应值“ELLO”等。

创建存储字符串每个字符地址的指针的正确方法是什么?

解决方案

一种方法以可视化的指针是对printf使用代替

printf("Value of line[%d] is: %c same as %c, Address is: %p, same as %p\n", i, line[i], *(ptr2line+i),ptr2line+i,&line[i]); 

,得到所需的输出

Value of line[0] is: H same as H, Address is: 0x7fff54ce6ade, same as 0x7fff54ce6ade 
Value of line[1] is: e same as e, Address is: 0x7fff54ce6adf, same as 0x7fff54ce6adf 
Value of line[2] is: l same as l, Address is: 0x7fff54ce6ae0, same as 0x7fff54ce6ae0 
Value of line[3] is: l same as l, Address is: 0x7fff54ce6ae1, same as 0x7fff54ce6ae1 
Value of line[4] is: o same as o, Address is: 0x7fff54ce6ae2, same as 0x7fff54ce6ae2 

另一种解决方案已经概述通过下述@Daniel怨妇。

回答

1

正如已经指出的那样,char *int *之间没有任何区别(当然指出类型除外)。你看到的不同的输出是因为the non member overloadsoperator<<这导致特殊处理charchar *

“看到” 的指针,就可以转换指针到void const *或直接调用成员函数(live on ideone):

#include <iostream> 

int main() { 
    char const * string = "Hello"; 
    std::cout << "string: " << string << std::endl 
     << "casted: " << static_cast<void const*>(string) << std::endl 
     << "member: "; 
    std::cout.operator<<(string); 
    std::cout << std::endl; 
} 

输出示例:

string: Hello 
casted: 0x2b1c038fbb7d 
member: 0x2b1c038fbb7d 
1

您创建的指针已将每个字符的地址存储在c字符串中。输出问题的方式是std::coutoperator<<处理c字符串。它将继续读取内存中的下一个条目,直到达到空字符\0。这就是为什么每行你都得到整个C字符串中剩余的字符。

编辑:为了更好地理解发生了什么,请参阅Daniel Jour的答案。