一般的なデータ型バイト
タイプ | 16位 | 32位 | 64位 |
---|---|---|---|
チャー | 1 | 1 | 1 |
短い整数 | 2 | 2 | 2 |
int | 2 | 4 | 4 |
符号なし整数 | 2 | 4 | 4 |
浮く | 4 | 4 | 4 |
ダブル | 8 | 8 | 8 |
長いです | 4 | 4 | 8 |
長い長い | 8 | 8 | 8 |
署名なしlong | 4 | 4 | 8 |
16ビットコンパイラ:char *:2バイト
32ビットエディター:char *:4バイト
64ビットエディター:char *:8バイト
const int * pとint * const pの違いは、コードを使用して分析されます。
#include <iostream>
using namespace std;
int main() {
int i1 = 40;
int i2 = 20;
const int *p;
p = &i1;
p = &i2; //不报错
*p = 80; //报错
//表明如果const在*前面表示*p是常量。
i2 = 80;
cout << *p << endl;
return 0;
}
#include <iostream>
using namespace std;
int main() {
int i1 = 40;
int i2 = 20;
//p = &i1; //报错
int * const p = &i1;
//p = &i2; //报错
*p = 80; //不报错
//表明如果const在*后面表示p是常量。
i2 = 80;
cout << *p << endl;
return 0;
}
3つの状況を追加
ケース1:int * p const int i1定数へのポインター
#include <iostream>
using namespace std;
int main() {
const int i1 = 20;
int *p;
//p = &i1; //报错,const int 类型的i1的地址是不能赋值给指向int类型地址的指针pi的,否
//则pi岂不是能修改i1的值了吗?
p = (int *) &i1;
cout << *p << endl;
*p = 80;
cout << *p << endl;
cout << i1 << endl;
return 0;
}
出力回答:
i1の値は変更されていません。
#include <iostream>
using namespace std;
int main() {
const int i1 = 20;
int *p;
p = (int *) &i1;
cout << p << endl;
cout << *p << endl;
*p = 80;
cout << p << endl;
cout << *p << endl;
cout << i1 << endl;
cout << &i1 << endl;
return 0;
}
出力:
理由は現在不明です。
ケース2:const int * piポインターがconst int i1を指す
2つのタイプは同じであり、次のプログラムに示すように割り当てることができます。i1は、piおよびi1で変更できません。
#include <iostream>
using namespace std;
int main() {
const int i1 = 20;
const int *p;
p = &i1;
cout << *p << endl;
return 0;
}
ケース3:const int * const p1を使用してポインターを宣言する
#include <iostream>
using namespace std;
int main() {
int i1 = 20;
int i2 = 10;
const int *const p = &i1;//必须在定义是初始化。
//p = &i2; //报错
//*p = 80; //报错
cout << *p << endl;
return 0;
}
ポインタ変数自体は、他の変数と同様にメモリアドレスにあります。このアドレスへのポインタを作成することもできます。
short int * p;
short int ** pp; short int *型のポインター変数のアドレスを格納(またはポイント)するために使用されるポインター変数ppを宣言します。
pp =&p;は、pのアドレスをppに割り当てることです。
例:
#include <iostream>
using namespace std;
int main() {
short int a;
short int *p;
short int **pp;
a = 10;
p = &a;
pp = &p;
cout << a << endl;
cout << *p << endl;
cout << **pp << endl;
return 0;
}
出力は次のとおりです。