user1376199
user1376199

Reputation: 51

error C2275 error with typedef struct

I have a program I am writing which is a basic image drawing program. It is in C.

Initialy i declare

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

I have a function to fill the background which accepts this so I use it like.

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

Now this works fine if it is the only thing in my main, but as soon as I add a switch/case and the rest of my code I can no longer use pixel_colour flood_colour = {80,50,91};

instead getting

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'

The main code is below, it works fine with all my functions until i try to use pixel_colour, It will be set to variable rather than 200,200,200 but even that 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;
  }

Upvotes: 5

Views: 2244

Answers (1)

MByD
MByD

Reputation: 137272

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

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 variables to be declared as needed.

Upvotes: 8

Related Questions