如何通过指针迭代二维数组?

发布于 2024-11-04 03:30:19 字数 1269 浏览 6 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

如果没结果 2024-11-11 03:30:19

首先,您收到警告的原因是 &theMatrix 的类型为 int(*)[20][20],而 theMatrixPointer类型为int *。你想要这个:

int *theMatrixPointer = &theMatrix[0][0];

其次,你得到垃圾的原因是因为你超出了数组的末尾。 while (theMatrixPointer) 将迭代直到 theMatrixPointer == 0。但请记住,theMatrixPointer一个地址。在您迭代整个地址空间并返回之前,这不会是0

你最好这样做:

int i, j;
for (i = 0; i < 20; i++)
{
    for (j = 0; j < 20; j++)
    {
        // matrixPointer[i][j] is the current element
    }
}

Firstly, the reason you get the warning is because &theMatrix is of type int(*)[20][20], whereas theMatrixPointer is of type int *. You want this:

int *theMatrixPointer = &theMatrix[0][0];

Secondly, the reason you are getting garbage is because you're going past the end of the array. while (theMatrixPointer) will iterate until theMatrixPointer == 0. But remember that theMatrixPointer is an address. This won't be 0 until you've iterated over the entire address space and wrapped back round!

You are probably best off doing this:

int i, j;
for (i = 0; i < 20; i++)
{
    for (j = 0; j < 20; j++)
    {
        // matrixPointer[i][j] is the current element
    }
}
两个我 2024-11-11 03:30:19

此处查看我对类似问题的回答。基本上,我认为处理 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].

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文