Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

160
Views
Is there a better way to compare two C structs?

Let's assume that I declared a C struct for holding configuration information, which has lots of fields(~30), e.g.:

struct config {
    int a;
    int b;
    int c;
    ...
    char *str1;
    char *str2;
    ...
};

What is the best way to compare two structs without simply enumerating and comparing each element? Of course I could use the following code but my question is, is there an easier way to achieve this?

static int
cmpInstances(struct config *l, struct config *r) {
    return (
        l->a == r->a &&
        l->b == r->b &&
        l->c == r->c &&
        !strcmp(l->str1, r->str1) &&
        !strcmp(l->str2, r->str2)
    );
}

Is there some macro that could do that? For example: cmpInstancesViaMacro(a,b,c) would expand into

   l->a == r->a &&
   l->b == r->b &&
   l->c == r->c
over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

A generalized variant of so-called X Macros could be used.

In the example of the question, it would look like this:

#define CONFIG_FIELDS \
  X_INT(a) \
  X_INT(b) \
  X_INT(c) \
  X_STRING(str1) \
  X_STRING(str2)

struct config {
  #define X_INT(NAME) int NAME;
  #define X_STRING(NAME) char *NAME;
    CONFIG_FIELDS
  #undef X_INT
  #undef X_STRING
};

static int cmpInstances(struct config *l, struct config *r) {
  #define X_INT(NAME) l->NAME == r->NAME &&
  #define X_STRING(NAME) strcmp(l->NAME, r->NAME) == 0 &&
    return CONFIG_FIELDS 1;
  #undef X_INT
  #undef X_STRING
}

With this solution, only the CONFIG_FIELDS macro needs to be changed when the structure is changed as long as there is no change to the set of types used. The structure and function would automatically be kept synchronized.

If a new type is introduced, the corresponding X Macros must be defined similar to X_INT and X_STRING above.

over 4 years ago · Santiago Trujillo Report

0

Perhaps some macros will help us reduce the lines of code and effort needed:

#define CMP(name) l->name == r->name &&
#define SCMP(name) !strcmp(l->name, r->name) &&

struct config
{
    int a;
    int b;
    int c;
    char *str1;
    char *str2;
};

static int cmpInstances(struct config *l, struct config *r) 
{
    return CMP(a) CMP(b) CMP(c) SCMP(str1) SCMP(str2) 1;
}

#undef CMP
#undef SCMP

Here we define some macros that take only the name of the variable and compare them. One is written for integer comparison (CMP) and the other is written for string comparison (SCMP). Then you can use these macros to compare your data and undef them when you are done.

Thanks to chqrlie for adding 1 part.

over 4 years ago · Santiago Trujillo Report

0

What is the best way to compare two structs without simply enumerating and comparing each element?

The presented way is the best. Except, the pointers should point to const - nothing is getting modified.

is there an easier way to achieve this?

The presented way is the easiest way to achieve it, it's also the most readable and self-explanatory, easy to refactor and reason about. Any other will add to complexity and result in maintenance burden.

I find strcmp(...) == 0 clearer then !strcmp(...).

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!