2017-05-27 20 views
1

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; 
} 

私は次の出力を得た:私はこのコードを実行した例

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

へ:

私はこの行を変更し

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; 
この場合はそうで「ello」に 0x7fff5054bad8対応「こんにちは」の値に対応し、0x7fff5054bad0 アドレスを

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 

私はこの出力を得ました。

文字列の各文字のアドレスを格納するポインタを作成する正しい方法は何ですか?ポインタを視覚化する

一つの方法は、代わりの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 *の間には違いはありません(もちろん、指し示すタイプを除く)。表示されている別の出力はがoperator<<であるため、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文字列を処理する方法です。メモリ内の次のエントリがNULL \0に達するまで読み込み続けます。そのため、各行では、残りの文字がC文字列全体に含まれていました。

EDIT:何が起こっているのかをよりよく理解するために、Daniel Jourの答えをご覧ください。