0

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.

#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}};
rktejesh
  • 70
  • 1
  • 9

1 Answers1

2

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.

#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 :

220 240 255
Krishna Kanth Yenumula
  • 2,533
  • 2
  • 14
  • 26