指针数组与数组指针
来源:互联网 发布:福田时代网络商学院 编辑:程序博客网 时间:2024/04/30 15:34
/* 指针数组(int *p[2]):列不确定
数组指针(int (*p)[2]):行不确定*/#include <iostream>using namespace std;int main(void){ //指针数组 int *p[2]; int a[4] = {11, 22, 33, 44}; int b[6] = {14, 25, 36, 47, 58, 69}; p[0] = a; p[1] = b; cout << *p[0] << endl; for(int i = 0; i < 4; i++) { cout << *(p[0] + i) << " "; } cout << endl; cout << *p[1] << endl; for(int i = 0; i < 6; i++) { cout << *(p[1] + i) << " "; } cout << endl; //数组指针 int (*q)[2]; int c[10][2] = {{1, 12}, {3, 14}, {5, 16}, {7, 18}, {9, 20}, {2,22}, {4, 24}, {6, 26}, {8, 28}, {10, 30}}; q = c; cout << q << endl; for(int i = 0; i < 10; i++) { cout << "yes" << (q+i) << endl; cout << "yes" << *(q+i) << endl; for(int j = 0; j < 2; j++) { //q[i]等价于*(q+i),所以*(*(q+i)+j)等价于*(q[i]+j),*(q[i]+j)等价于q[i][j] cout << *(*(q+i)+j) << " "; cout << q[i][j] << " "; } cout << endl; }}