【发布时间】:2011-08-15 00:55:00
【问题描述】:
我有一个 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;
}
...随着时间的推移而中断,因为数据是垃圾(不在矩阵中的大整数)。那么,如何正确地用指针迭代这个矩阵呢?
【问题讨论】: