在 C++ 中声明结构类型的二维数组 [英] Declaring a 2D array of type struct in c++

查看:57
本文介绍了在 C++ 中声明结构类型的二维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我声明了一个应该是像素的结构体,它有 3 个属性(xy 位置和 F 强度),例如这个:

I declared a struct which is supposed to be a pixel and it has 3 properties (x, y location and F intensity) like this:

struct pixel {
    int F,          // intensity from 0-255
    x,              // horizontal component           
    y;              // vertical component
};

然后我像这样声明了一个像素类型的二维数组(上面的结构):

Then I declared an 2D array of type pixel (the struct above) like this:

int N=100;
pixel image[N][N];

然后我使用以下循环为 xy 赋值:

Then I used the following loop to assign values to x and y:

int count, k;

for (int i=0 ; i<N ; i++)
    for (int j=0 ; j<N ; j++)
    {
        k = j + i*N;
        image.x[k] = count;
        count++; 
     }

我做错了什么?

推荐答案

image.x[k] = count;

不正确.您声明了一个二维像素数组:

is incorrect. You declared a 2D array of pixels:

pixel image[N][N];

访问数组元素的方式如下:

The way to access an element of the array is as follows:

image[i][j].x = count;

您不需要自己计算平坦指数 k.

You do not need to calculate the flat index k yourself.

这篇关于在 C++ 中声明结构类型的二维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆