定义以下变量:
char a[4][3][2] = {{{'a', 'b'}, {'c', 'd'}, {'e', 'f'}},
{{'g', 'h'}, {'i', 'j'}, {'k', 'l'}},
{{'m', 'n'}, {'o', 'p'}, {'q', 'r'}},
{{'s', 't'}, {'u', 'v'}, {'w', 'x'}}};
char (*pa)[2] = &a[1][0];
char (*ppa)[3][2] = &a[1];
要想通过pa或ppa访问数组a中的'r'元素,分别应该怎么写?/*
* File: main.cpp
* Author: annhe
*
* Created on 201年月日, :
*/
#include <cstdlib>
#include <iostream>
using namespace std;
int main(int argc, char* argv[])
{
char a[4][3][2] = {{{'a', 'b'}, {'c', 'd'}, {'e', 'f'}},
{{'g', 'h'}, {'i', 'j'}, {'k', 'l'}},
{{'m', 'n'}, {'o', 'p'}, {'q', 'r'}},
{{'s', 't'}, {'u', 'v'}, {'w', 'x'}}};
char (*pa)[2] = &a[1][0];
char (*ppa)[3][2] = &a[1];
//pa为一个指向具有两个元素的数组的指针,赋值&a[1][0]后指向{'g','h'},所以pa[5]=={'q','r'},pa[5] [1]=='r';
cout<<"pa[5][1] :"<<pa[5][1]<<endl;
//ppa指向{{'g','h'},{'i','j'},{'k','l'}},ppa[1]=={{'m','n'}, {'o','p'},{'q','r'}},ppa[1][2]=={'q','r'},ppa[1][2][1]=='r';
cout<<"ppa[1][2][1] :"<<ppa[1][2][1]<<endl;
cout<<"*(*(pa+5)+1) :"<<*(*(pa+5)+1)<<endl;
cout<<"*(*(*(ppa+1)+2)+1) :"<<*(*(*(ppa+1)+2)+1)<<endl;
return 0;
}
|