-1

I have a question about using multiple boolean values in a struct.

I have an struct

typedef struct Foo
{
 foo2,
 foo3
}

which is a part of another main struct

typedef struct MainFoo
{
  int n,
  ....
  .
  .
  FOO foo

and function

void foo(int foo)
{
   if(foo)
   b = true;
}

and in another function :

if(b)
{
 bool Foo.foo2 = true;
}

if(b)
{
  bool Foo.foo3 = true;
}

and finally in the another class I check

if(Foo.foo2)
{
  //Do something
}

if(Foo.foo3)
{
 //Do Something
}

So My question is if there is any other better way to check these. I tried using Dword but not sure if I understood it but it just got the last boolean state that was checked and hence couldn't check for each boolean one at a time in another class. Any suggestion in this? Is using the struct just for the bool a good practice? If not is there any other better way?

Thank you very much

Jasmine
  • 135
  • 3
  • 16

1 Answers1

3

It mainly depends on your problem. Briefly, use the Foo struct only if it has some meaning outside MainFoo as a whole (for example, if they are all settings that should be transferred all together or saved to a file externally), otherwise move foo2 and foo3 to MainFoo.

Except for that, the use is correct. On the other hand, you can reduce memory consumption if you use bit fields.

struct Foo {
  bool foo2 : 1;
  bool foo3 : 1;
};
cbuchart
  • 10,847
  • 9
  • 53
  • 93
  • PS: you may wish to take a look at the difference between `typedef struct` and `struct` [in this answer](http://stackoverflow.com/a/612350/1485885). – cbuchart Mar 12 '17 at 11:31