C-尝试访问数组成员

时间:2018-10-15 21:58:37

标签: c arrays

我想做的是访问代表RGB565(16位)图像的数组元素,并尝试将其转换为RGB888(24位)。

数组如下:

.h文件中的代码

UG_BMP  logo_config_placas;

.c文件中的代码

UG_BMP logo_config_placas = {
     (void*)&logo_config_placas_i,
     245 ,
     231,
     BMP_BPP_16,
     BMP_RGB565  };

const UG_U16 logo_config_placas_i[] = {
     0x0,0x0,0x0,0x0,0x0,0x0,0x0 ... }

我知道显示的这些数字仅为零(此角为黑色),但除此之外还有其他颜色。

我想做的是从数组的每个元素中获取rgb565颜色,并将其转换为rgb888,以便我可以在显示器上打印它。数字231和245是因为图像的大小(宽度245像素,高度231像素)。

for (int j = 0; j < 231; j++) {
    for (int i = 0; i < 245; i++) {
        UG_COLOR rgb16 = (UG_COLOR)*(logo_config_placas.p + i + (245 * j));
        UG_COLOR rgb24 = ((((rgb16 & 0x0000F800) << 8) | ((rgb16 & 0x000007E0) << 5)) | ((rgb16 & 0x0000001F) << 3)) & 0x00FFFFFF;
        Print_Pixel(2 + i, 38 + j, rgb24);
    }
}

我只是不断收到警告和错误,例如:

  

错误:无效使用void表达式

     

警告:尊重“ void *”指针

我确定错误与rgb16变量一致,但是我无法弄清楚如何正确调用数组的元素。

1 个答案:

答案 0 :(得分:0)

我想logo_config_placas.p是结构的第一个元素。如果是这样,那是因为您在做:

logo_config_placas.p 

这意味着您正在.p上进行void *(根据您在logo_config_placas中的情况)

问题的最小模拟:

struct A
{
        int a;
} sa;

struct B
{
        void *a;
} sb = {(void *)&sa};

int main()
{       
        int c = (int) *(sb.a);
}       

结果为:

a.c: In function ‘main’:
a.c:15:16: warning: dereferencing ‘void *’ pointer
  int c = (int) *(sb.a);
                ^~~~~~~
a.c:15:10: error: invalid use of void expression
  int c = (int) *(sb.a);

这是不言自明的。

您首先需要将.p投射到您使用的任何位置。