简体   繁体   English

二维结构数组的赋值

[英]Assignment of a 2 dimensional struct array

I have defined a new struct type called RGBTRIPLE but I'm not able to assign values to it.I'm trying to create a test file to test the different parts of blur code in cs50.我已经定义了一个名为 RGBTRIPLE 的新结构类型,但我无法为其分配值。我正在尝试创建一个测试文件来测试 cs50 中模糊代码的不同部分。

#include <stdio.h>
#include <stdint.h>

typedef uint8_t  BYTE;

typedef struct
{
    BYTE rgbtBlue;
    BYTE rgbtGreen;
    BYTE rgbtRed;
}
RGBTRIPLE;



int main(void)
{
    int height = 3;
    int width = 3;
    RGBTRIPLE image[3][3];
    
    image[2][2].rgbtRed = {{10, 40, 70},{110, 120, 130},{200, 220, 240}};
    image[2][2].rgbtGreen = {{20, 50, 80},{130, 140, 150},{210, 230, 250}};
    image[2][2].rgbtBlue = {{30, 60, 90},{140, 150, 160},{220, 240, 255}};
    
}

I am getting a error我收到一个错误

2.c:22:24: error: expected expression
        image[2][2].rgbtRed = {{10, 40, 70},{110, 120, 130},{200, 220, 240}};
                              ^
2.c:23:26: error: expected expression
        image[2][2].rgbtGreen = {{20, 50, 80},{130, 140, 150},{210, 230, 250}};
                                ^
2.c:24:25: error: expected expression
        image[2][2].rgbtBlue = {{30, 60, 90},{140, 150, 160},{220, 240, 255}};

You should initialize the values at the time of declaration.您应该在声明时初始化这些值。 You cannot initialize like that ( as in your code).你不能像那样初始化(就像你的代码一样)。 That is a invalid C syntax.这是无效的 C 语法。

#include <stdio.h>
#include <stdint.h>

typedef uint8_t  BYTE;

typedef struct
{
    BYTE rgbtBlue;
    BYTE rgbtGreen;
    BYTE rgbtRed;
}
RGBTRIPLE;

int main(void)
{
    int height = 3;
    int width = 3;
    RGBTRIPLE image[3][3] = {{{10, 40, 70},{110, 120, 130},{200, 220, 240}},{{20, 50, 80},{130, 140, 150},{210, 230, 250}},{{30, 60, 90},{140, 150, 160},{220, 240, 255}}};
    
    printf("%d ",image[2][2].rgbtBlue);
    printf("%d ",image[2][2].rgbtGreen);
    printf("%d ",image[2][2].rgbtRed);

    return 0;
}

The output is: output 是:

220 240 255

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM