- 指针的定义与语法
- 指针与数组
- 指针与二维数组
- 指针变量
指针的语法与赋值
指针的语法:数据类型 *指针名
定义:指针内容为数据的内存地址
eg:
void charge(int a, int b) { swap(a, b); } void ncharge(int *a, int *b) { swap(*a, *b); } int main() { int a = 1, b = 2; charge(a, b); cout << a << " " << b<<endl; ncharge(&a, &b); cout << a << " " << b << endl; }
一个指针的指向不确定时
int *n= nullptr; // nullptr为空,即指针为0
指针与数组
整数
int* a; int data[4] = { 1,2,3,4 }; a = data; cout << a << endl; cout << a[3] << endl; cout << *a+3 << endl;
字符串
int main() { const char* a="hello"; cout << a << endl; cout << *(a+1) << endl;
cout << a[1] << endl; }
指针数据在内存中的大小
int main() { char* a; int* p; double* da; float* fa; void* va; cout << sizeof(a)<<endl; cout << sizeof(p) << endl; cout << sizeof(da) << endl; cout << sizeof(fa) << endl; cout << sizeof(va) << endl; }
指针与二维数组
二维数组指针语法:类型(*变量名)[整形常量]
int main() { int(*a)[3]; int data[2][3] = { 1,2,3,4,5,6 }; a = data; for (int i = 0; i < 2; i++) { for (int j = 0; j < 3; j++) { cout << a[i][j]<<" "; } cout << endl; }}
int main() { int(*a)[3]; int data[2][3] = { {1,2},{3}}; a = data; for (int i = 0; i < 2; i++) { for (int j = 0; j < 3; j++) { cout << a[i][j]<<" "; } cout << endl; }}
指针变量
语法结果:数据类型 ** 变量名
标签:cout,int,指针,++,main,data,一级 From: https://www.cnblogs.com/20wod/p/17367106.html