Skip to main content
Code Review

Return to Revisions

3 of 3
Commonmark migration

Return status

You create this:

typedef int STATUS;
#define ERROR -1
#define OKAY 0

which is basically a boolean status. Personally, I'd return a straight bool.

Bug/Not what you mean

Doing a

result = NULL;

is changing the local variable (parameter) result. It's not setting the result to NULL. In fact the caller won't probably have a pointer at all, but just a bool, which cannot properly be NULL.

Shorter version

I'm not sure this is cleaner, but here I go:

bool is_less_than(struct version * original, struct version *compared, bool *result)
{
 if(original == NULL || compared == NULL || result == NULL)
 return false;
 
 *result = original->major < compared->major || original->major == compared->major && (
 original->minor < compared->minor || original->minor == compared->minor && (
 original->build < compared->build || original->build == compared->build && (
 original->patch < compared->patch)));
 return true;
}

Next time, add a driver/test suite to your question, to ease the life of people answering. This can be one:

int main(void) 
{
 struct version ref = { 1, 2, 21, 8 };
 struct version lower1 = { 0, 2, 21, 8 };
 struct version lower2 = { 1, 1, 21, 8 };
 struct version lower3 = { 1, 2, 20, 8 };
 struct version lower4 = { 1, 2, 21, 7 };
 struct version equal = { 1, 2, 21, 8 };
 struct version higher1 = { 2, 2, 21, 8 };
 struct version higher2 = { 1, 3, 21, 8 };
 struct version higher3 = { 1, 2, 22, 8 };
 struct version higher4 = { 1, 2, 21, 9 };
#define TEST(a,b,expect1,expect2)\
 do {\
 bool result1, result2;\
 is_less_than((a), (b), &result1);\
 is_less_than((b), (a), &result2);\
 puts(result1==(expect1) && result2==(expect2)?"ok":"failed");\
 } while(0)
#define TESTL(a,b) TEST(a,b,true,false)
#define TESTE(a,b) TEST(a,b,false,false)
#define TESTH(a,b) TEST(a,b,false,true)
 TESTL(&lower1, &ref);
 TESTL(&lower2, &ref);
 TESTL(&lower3, &ref);
 TESTL(&lower4, &ref);
 TESTE(&equal, &ref);
 TESTH(&higher1, &ref);
 TESTH(&higher2, &ref);
 TESTH(&higher3, &ref);
 TESTH(&higher4, &ref);
 return 0;
}
default

AltStyle によって変換されたページ (->オリジナル) /