[OT][GEEK] Happy new year.

S

striker

#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!
 
A

Arthur J. O'Dwyer

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... ;-)
#define YEARDAYS ((ano % 4 == 0) && ano % 100 != 0) || ano % 400 == 0) ?\
^ Missing '('
366 : 365

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!
year * newYear {
static year *actual;

Style point: Missing explicit initialization of 'actual'. Adding
the '= NULL' couldn't hurt.
if(actual != NULL) free(actual);
return actual = (year) malloc(sizeof(day) * YEARDAYS);

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!
}

Happy new year!

It's always New Year's somewhere on the Internet, I guess... :)

-Arthur
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top