Search code examples
cif-statementstructureansi-c

Selecting a structure via IF statement


I am trying to select one of two possible ANSI C expressions using an IF statement. Each expression works fine by itself, eg...

wb_Parameters *WeightLimits = set100Parameters();

but when they're selected via an IF eg...

if (strcmp(CurrentAircraft->PhenomType,"100") == 0) 
    wb_Parameters *WeightLimits = set100Parameters();
else
    wb_Parameters *WeightLimits = set300Parameters();

I get the error message "Use of undeclared identifier 'WeightLimits'." What do I need to do to make this work inside an IF statement?


Solution

  • WeightLimits goes out of scope after the if statement. To avoid that, declare it before the if:

    wb_Parameters *WeightLimits;
    if (strcmp(CurrentAircraft->PhenomType,"100") == 0)
        WeightLimits = set100Parameters();
    else
        WeightLimits = set300Parameters();