深入解析int(*p)[]和int(**p)[]
更新时间:2013年07月11日 10:50:47 作者:
以下是对int(*p)[]和int(**p)[]的使用进行了详细的分析介绍,需要的朋友可以参考下
1. int(*p)[10]:
根据运算符的结合律,()的优先级最高,所以p是一个指针,指向的一个维度为10的一维数组。
p一个指向数组的某一行
int a[1][4]={1,2,3,4};
int (*p)[4] = a;//p point to the row of array a
for(int i=0;i<4;i++)
{
cout<<*((*p)+i)<<" ";
}
2. int(**q)[10]
这个的意义:q是一个指针,指向的元素就是1.中的p.
下面给一个例子:
#include<iostream>
#include<stdio.h>
using namespace std;
int main()
{
int a[2][2]={1,2,3,4};
int (*p)[2] = a;//p point to the row of array a
for(int i = 0;i<2;i++)//output matrix using p
{
for(int j = 0;j<2;j++)
{
cout<<*(*(p+i)+j)<<" ";
}
cout<<endl;
}
int (**q)[2] = &p;//q point to p
for(int i = 0;i<2;i++)//output matrix using q
{
for(int j = 0;j<2;j++)
{
cout<<*(*(*q+i)+j)<<" ";
}
cout<<endl;
}
getchar();
return 0;
}
根据运算符的结合律,()的优先级最高,所以p是一个指针,指向的一个维度为10的一维数组。
p一个指向数组的某一行
复制代码 代码如下:
int a[1][4]={1,2,3,4};
int (*p)[4] = a;//p point to the row of array a
for(int i=0;i<4;i++)
{
cout<<*((*p)+i)<<" ";
}
2. int(**q)[10]
这个的意义:q是一个指针,指向的元素就是1.中的p.
下面给一个例子:
复制代码 代码如下:
#include<iostream>
#include<stdio.h>
using namespace std;
int main()
{
int a[2][2]={1,2,3,4};
int (*p)[2] = a;//p point to the row of array a
for(int i = 0;i<2;i++)//output matrix using p
{
for(int j = 0;j<2;j++)
{
cout<<*(*(p+i)+j)<<" ";
}
cout<<endl;
}
int (**q)[2] = &p;//q point to p
for(int i = 0;i<2;i++)//output matrix using q
{
for(int j = 0;j<2;j++)
{
cout<<*(*(*q+i)+j)<<" ";
}
cout<<endl;
}
getchar();
return 0;
}
相关文章
C++命名空间using namespace std是什么意思
namespace中文意思是命名空间或者叫名字空间,传统的C++只有一个全局的namespace,下面这篇文章主要给大家介绍了关于C++命名空间using namespace std是什么意思的相关资料,需要的朋友可以参考下2023-01-01
VS报错C6011的问题:取消对NULL指针的引用(解决方法)
这篇文章主要介绍了VS报错C6011的问题:取消对NULL指针的引用(解决方法),C6011:取消对NULL指针的引用,发现是没有进行空指针的判断,解决方案跟随小编一起看看吧2024-01-01


最新评论