如何通过指针迭代二维数组?
我有一个 20x20 矩阵。我想从矩阵中提取数据块。我有
int theMatrix[20][20] = {};//Full code initializes with #'s
int *theMatrixPointer = &theMatrix;
但是然后我收到编译器警告说
警告:初始化来自 不兼容的指针类型
我继续运行代码,它看起来从左到右穿过矩阵。至少在短期内是这样。实现:
//left to right with pointer;
while(theMatrixPointer)
{
int one = 0;
int two = 0;
int three = 0;
int four = 0;
double currentProduct = 0;
//int stop = 0;
for(int i = 0; i < depth; i++)
{
/*if(!theMatrixPointer)
{
stop = 1;
break;
}*/
int currentValue = *theMatrixPointer++;
printf("%d\n",currentValue);
if(i == 0)
{
one = currentValue;
}
else if (i == 1)
{
two = currentValue;
}
else if (i == 2)
{
three = currentValue;
}
else if (i == 3)
{
four = currentValue;
}
}
/*if(stop)
break;*/
currentProduct = one * (double)two * three * four;
printf("PRODUCT: %f\n",currentProduct);
startPoint++;
theMatrixPointer = startPoint;
}
...随着时间的推移而中断,因为数据是垃圾(不在矩阵中的大整数)。那么,如何用指针正确迭代这个矩阵呢?
I have a 20x20 matrix. I want to extract chunks of data from the matrix. I have
int theMatrix[20][20] = {};//Full code initializes with #'s
int *theMatrixPointer = &theMatrix;
But then I get a compiler warning saying
warning: initialization from
incompatible pointer type
I went ahead and ran the code and it looks to be moving across the matrix from left to right. At least in the short run. The implementation:
//left to right with pointer;
while(theMatrixPointer)
{
int one = 0;
int two = 0;
int three = 0;
int four = 0;
double currentProduct = 0;
//int stop = 0;
for(int i = 0; i < depth; i++)
{
/*if(!theMatrixPointer)
{
stop = 1;
break;
}*/
int currentValue = *theMatrixPointer++;
printf("%d\n",currentValue);
if(i == 0)
{
one = currentValue;
}
else if (i == 1)
{
two = currentValue;
}
else if (i == 2)
{
three = currentValue;
}
else if (i == 3)
{
four = currentValue;
}
}
/*if(stop)
break;*/
currentProduct = one * (double)two * three * four;
printf("PRODUCT: %f\n",currentProduct);
startPoint++;
theMatrixPointer = startPoint;
}
...breaks over time as the data is garbage (big ints that are not in the matrix). So, how can I properly iterate this matrix with a pointer?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,您收到警告的原因是
&theMatrix
的类型为int(*)[20][20]
,而theMatrixPointer
类型为int *
。你想要这个:其次,你得到垃圾的原因是因为你超出了数组的末尾。
while (theMatrixPointer)
将迭代直到theMatrixPointer == 0
。但请记住,theMatrixPointer
是一个地址。在您迭代整个地址空间并返回之前,这不会是0
!你最好这样做:
Firstly, the reason you get the warning is because
&theMatrix
is of typeint(*)[20][20]
, whereastheMatrixPointer
is of typeint *
. You want this:Secondly, the reason you are getting garbage is because you're going past the end of the array.
while (theMatrixPointer)
will iterate untiltheMatrixPointer == 0
. But remember thattheMatrixPointer
is an address. This won't be0
until you've iterated over the entire address space and wrapped back round!You are probably best off doing this:
此处查看我对类似问题的回答。基本上,我认为处理 theMatrix[20*20] 是比处理 theMatrix[20][20] 更明智的默认方法。
Check my answer to a similar question here. Basically, I think that dealing with theMatrix[20*20] is a wiser default approach than dealing with theMatrix[20][20].