Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7719533
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T03:26:25+00:00 2026-06-01T03:26:25+00:00

I have a program that compares variables from two structs and sets a bit

  • 0

I have a program that compares variables from two structs and sets a bit accordingly for a bitmap variable. I have to compare each variables of the struct. No. of variables in reality are more for each struct but for simplicity I took 3. I wanted to know if i can create a macro for comparing the variables and setting the bit in the bitmap accordingly.

#include<stdio.h>


struct num 
{
   int a;
   int b;
   int c;
};

struct num1
{
   int d;
   int e;
   int f;
};

enum type
{
   val1 = 0,
   val2 = 1,
   val3 = 2,
};
int main()
{
  struct num obj1;
  struct num1 obj2;
  int bitmap = 0;

  if( obj1.a != obj2.d)
  {
      bitmap  = bitmap | val1;
  }
  if (obj1.b != obj2.e)
     bitmap = bitmap | val2;

  printf("bitmap - %d",bitmap);
  return 1;


}

can i declare a macro like…

#define CHECK(cond)
  if (!(cond))
    printf(" failed check at %x: %s",__LINE__, #cond);
    //set the bit accordingly

#undef CHECK
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T03:26:27+00:00Added an answer on June 1, 2026 at 3:26 am

    With a modicum of care, you can do it fairly easily. You just need to identify what you’re comparing and setting carefully, and pass them as macro parameters. Example usage:

    CHECK(obj1.a, obj2.d, bitmap, val1);
    CHECK(obj1.b, obj2.e, bitmap, val2);
    

    This assumes that CHECK is defined something like:

    #define STRINGIFY(expr) #expr
    
    #define CHECK(v1, v2, bitmap, bit) do \
        { if ((v1) != (v2)) \
             {   printf("failed check at %d: %s\n", __LINE__, STRINGIFY(v1 != v2)); \
                 (bitmap) |= (1 << (bit)); \
             } \
        } while (0)
    

    You can lay the macro out however you like, of course; I’m not entirely happy with that, but it isn’t too awful.

    Demo Code

    Compilation and test run:

    $ gcc -Wall -Wextra -g -O3 -std=c99 xx.c -o xx && ./xx
    failed check at 40: obj1.a != obj2.d
    failed check at 42: obj1.c != obj2.f
    bitmap - 5
    $
    

    Actual code:

    #include <stdio.h>
    
    struct num 
    {
       int a;
       int b;
       int c;
    };
    
    struct num1
    {
       int d;
       int e;
       int f;
    };
    
    enum type
    {
       val1 = 0,
       val2 = 1,
       val3 = 2,
    };
    
    #define STRINGIFY(expr) #expr
    
    #define CHECK(v1, v2, bitmap, bit) do \
        { if ((v1) != (v2)) \
          {   printf("failed check at %d: %s\n", __LINE__, STRINGIFY(v1 != v2)); \
          (bitmap) |= (1 << (bit)); \
          } \
        } while (0)
    
    
    int main(void)
    {
        struct num  obj1 = { 1, 2, 3 };
        struct num1 obj2 = { 2, 2, 4 };
        int bitmap = 0;
    
        CHECK(obj1.a, obj2.d, bitmap, val1);
        CHECK(obj1.b, obj2.e, bitmap, val2);
        CHECK(obj1.c, obj2.f, bitmap, val3);
    
        printf("bitmap - %X\n", bitmap);
        return 0;
    }
    

    Clearly, this code relies on you matching the right elements and bit numbers in the invocations of the CHECK macro.

    It’s possible to devise more complex schemes using offsetof() etc and initialized arrays describing the data structures, etc, but you’d end up with a more complex system and little benefit. In particular, the invocations can’t reduce the parameter count much. You could assume ‘bitmap’ is the variable. You need to identify the two objects, so you’ll specify ‘obj1’ and ‘obj2’. Somewhere along the line, you need to identify which fields are being compared and the bit to set. That could be some single value (maybe the bit number), but you’ve still got 3 arguments (CHECK(obj1, obj2, valN) and the assumption about bitmap) or 4 arguments (CHECK(obj1, obj2, bitmap, valN) without the assumption about bitmap), but a lot of background complexity and probably a greater chance of getting it wrong. If you can tinker with the code so that you have a single type instead of two types, etc, then you can make life easier with the hypothetical system, but it is still simpler to handle things the way shown in the working code, I think.

    I concur with gbulmer that I probably wouldn’t do things this way, but you did state that you had reduced the sizes of the structures dramatically (for which, thanks!) and it would become more enticing as the number of fields increases (but I’d only write out the comparisons for one pair of structure types once, in a single function).

    You could also revise the macro to:

    #define CHECK(cond, bitmap, bit) do \
        { if (cond) \
          {   printf("failed check at %d: %s\n", __LINE__, STRINGIFY(cond)); \
              (bitmap) |= (1 << (bit)); \
          } \
        } while (0)
    
    CHECK(obj1.a != obj2.d, bitmap, val1);
    ...
    CHECK((strcmp(obj3.str1, obj4.str) != 0), bitmap, val6);
    

    where the last line shows that this would allow you to choose arbitrary comparisons, even if they contain commas. Note the extra set of parentheses surrounding the call to strcmp()!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a program that compares files in two folders. I want to detect
I have program that has a variable that should never change. However, somehow, it
I have a program that uses the mt19937 random number generator from boost::random. I
I have a program that runs osql.exe from microsoft sql server tools directory and
I'm currently writing a program that needs to compare each file in an ArrayList
I have a list of ID numbers that I pull from a session variable
I have a program that simulates dice rolls and compares them to values in
THIS IS HOMEWORK: I have a program that consists of two winforms and three
I have a C++ program that will read in data from a binary file
I have program that runs fast enough. I want to see the number of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.