r/programminghorror • • Apr 22 '26

switch case abuse

char g  = '0';// ypr
char gg = '0';// pid
double ggg;
const uint8_t _ = 11;
scanf("%c %c %f/n", g, gg, ggg);
if ((g  == 'y' || g  == 'p' || g  == 'r') && 
    (gg == 'p' || gg == 'i' || gg == 'd')) {
    switch   ((( gg << 2) & ~_) | ((g) & _)) {
        case ((('p' << 2) & ~_) | ('y' & _)):
            p_gain_y = ggg;
            break;
        case ((('i' << 2) & ~_) | ('y' & _)):
            i_gain_y = ggg;
            break;
        case ((('d' << 2) & ~_) | ('y' & _)):
            d_gain_y = ggg;
            break;
        case ((('p' << 2) & ~_) | ('p' & _)):
            p_gain_p = ggg;
            break;
        case ((('i' << 2) & ~_) | ('p' & _)):
            i_gain_p = ggg;
            break;
        case ((('d' << 2) & ~_) | ('p' & _)):
            d_gain_p = ggg;
            break;
        case ((('p' << 2) & ~_) | ('r' & _)):
            p_gain_r = ggg;
            break;
        case ((('i' << 2) & ~_) | ('r' & _)):
            i_gain_r = ggg;
            break;
        case ((('d' << 2) & ~_) | ('r' & _)):
            d_gain_r = ggg;
            break;
    }
}                                                
46 Upvotes

26 comments sorted by

View all comments

Show parent comment

1

u/bzzzt_beep Apr 24 '26

yes , the bug is that he is using the original initialized values of the variable, rather than the values read from the scanf into memory address '0' (0x30)
also, as u/Ok_Chemistry_6387 noted , address 0x30 might not be writable.

u/Ok_Chemistry_6387 in other comments is just being smartly sarcastic about the unnecessary hard work being done to save memory address space and cpu cycles.