Error C2275 with typedef structure

I have a program that I am writing, which is the basic program for drawing images. It is located in C.

I initially declare

typedef struct { int red; int green; int blue; } pixel_colour; 

I have a function to fill the background that accepts this, so I use it as.

 pixel_colour flood_colour = {80,50,91}; FloodImage(flood_colour); 

Now this works great if it's the only one in my life, but as soon as I add the key / block and the rest of my code, I can no longer use pixel_colour flood_colour = {80,50,91};

instead of this

 error C2275: 'pixel_colour' : illegal use of this type as an expression 1> c:\users\xxxx\documents\visual studio 2010\projects\xxx.c(20) : see declaration of 'pixel_colour' 

Below is the main code, it works fine with all my functions until I try to use pixel_colour. It will be set to a variable, not 200,200,200, but even this does not work.

 char instring[80] = "FL 201 3 56"; int pst = FirstTwo(instring); switch( pst ) { case 1: printf( "FL "); CaseFL(instring); pixel_colour flood_colour = {200,200,200}; FloodImage(flood_colour); break; case 2: printf( "LI" ); break; case 3: printf( "RE" ); break; case 4: printf( "CH" ); break; case 5: printf( "FI" ); break; case 6: printf( "EX" ); exit(EXIT_FAILURE); break; default : printf( "Something went wrong" ); break; } 
+4
source share
1 answer

In C89, supported by MSVC, you can only declare a variable at the beginning of the code. Instead, you can:

 case 1: { // first thing in the block - variable declaration / initialization pixel_colour flood_colour = {200,200,200}; printf( "FL "); CaseFL(instring); FloodImage(flood_colour); break; } 

C99, C11 and C ++ all allow you to declare variables as necessary.

+7
source

Source: https://habr.com/ru/post/1410805/


All Articles