文字にアクセスする際や明示的にASCII番号に変換するときに問題があります。考えてみましょう以下のコード:文字の値を返すべき、p[4]
とp[2]
を有する発現にもかかわらず、p=p+p[4]-p[2]
を使用しているとき、私はコンパイルエラーを取得しないC++のポインターによる文字配列へのアクセス
include<iostream>
using namespace std;
int main()
{
char* p;
char s[100]="hello world";
p=s;
cout<<p[2]<<endl;//This gives the actual character from the string "hello
//world": "l"
p=p+p[4]-p[2];//But this does something different, it basically uses
//ASCII values of the characters at p[4] and p[2] and uses them to do
//pointer arithmetic
cout<<p;// gives "lo world"
return 0;
}
"私の質問です:なぜ" cout <" - 何ですか? –
ヒント:文字は実際にはちょうど小さな(小)整数です –
std :: stringを使用してください –