[OT][GEEK] Happy new year.

Discussion in 'C Programming' started by striker, Dec 28, 2003.

  1. striker

    striker Guest

    #define YEARDAYS ((ano % 4 == 0) && ano % 100 != 0) || ano % 400 == 0) ?\
    366 : 365

    year * newYear {
    static year *actual;

    if(actual != NULL) free(actual);
    return actual = (year) malloc(sizeof(day) * YEARDAYS);
    }

    Happy new year!
     
    striker, Dec 28, 2003
    #1
    1. Advertisements

  2. If you post C code to comp.lang.c, EVEN JOKE CODE, you're gonna
    get people like me who ignore the [OT] tags and plunge ahead... ;-)
    ^ Missing '('
    Also, where is 'ano' defined? Much better practice would be to
    take the value of 'ano' as an argument to a function-like macro,
    YEARDAYS(foo).
    Finally, you're missing a set of parentheses around the text of
    the macro. Try compiling your original snippet; if sizeof(day) is
    anything other than 1, you'll have a buffer overrun waiting to
    happen. Operator precedence, man!
    Style point: Missing explicit initialization of 'actual'. Adding
    the '= NULL' couldn't hurt.
    Casting 'malloc' is BAD, because it can hide ERRORS like the pair
    you have here. First, of course, you need to #include <stdlib.h>
    for both 'free' and 'malloc' to work; and secondly, you're trying to
    cast (void *) to (year), which may or may not work, depending on the
    actual type of 'year'. You meant (year *) -- but you could have
    written absolutely no cast at all, and been perfectly correct and
    more readable to boot!
    It's always New Year's somewhere on the Internet, I guess... :)

    -Arthur
     
    Arthur J. O'Dwyer, Dec 28, 2003
    #2
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.