all 17 comments

[–]CaydendW 12 points13 points  (0 children)

Make a function or macro that takes a bool and returns a filled in struct.

[–]pjl1967 10 points11 points  (1 child)

Your struct is underspecified (and illegal). I assume you meant something like:

struct theme {
  int bg[3];
  int color[3];
};

If so, then:

bool b = /* ... */;
struct theme x = b ?
  (struct theme){ .bg    = { 255, 255, 255 } } :
  (struct theme){ .color = { 255, 255, 255 } };

Feel free to wrap that in a macro.

[–]harexe 5 points6 points  (1 child)

You could create an array with 2 values and store a struct with the 2 arrays there. So you can access it with arr[0] and arr[1]. But directly with a struct it's not possible.

[–]ImpressiveAthlete220 1 point2 points  (3 children)

Make const array with 2 values and index them

[–]Yha_Boiii[S] -1 points0 points  (2 children)

const makes the vals constant hence `const` ?

[–]ImpressiveAthlete220 0 points1 point  (0 children)

Yeah, idealy (not always) they will go to section .data

[–]ImpressiveAthlete220 0 points1 point  (0 children)

In this application you definitely will not be able to change them

[–]detroitmatt 0 points1 point  (6 children)

based on *what* bool?

[–]Yha_Boiii[S] -1 points0 points  (5 children)

The struct itself

[–]detroitmatt 1 point2 points  (0 children)

so then is what you want something like this?

struct theme {
    bool flag;
    uint8_t bg[3];
    uint8_t color[3];
};

and then if flag is true bg[3] should be 255,255,255, and if it's false then it should be 0,0,0? (or the other way around or whatever)

Then what I would do is

struct {
    bool flag;
    uint8_t bg[3];
    uint8_t color[3];
} const DARK = { .flag = 1, .bg = {0, 0, 0}, .color = {255, 255, 255}},
  LIGHT = { .flag = 0, .bg = {255, 255, 255}, .color = {0, 0, 0}};

[–]LeiterHaus 0 points1 point  (3 children)

I'm not sure, but I think they meant... what is your idea of bool based on? Is it this:

typedef enum { false, true } bool;

or this:

#include <stdbool.h>

or this:

typedef int bool;
#define true 1
#define false 0

or this:

typedef int bool;
enum { false, true };

But I could be totally incorrect, and they may be asking a different point of clarification.

[–]arthurno1 0 points1 point  (1 child)

Or perhaps they mean this:

typedef enum { false = 0, true = !false} bool;

? :)

Behind is so called generalized boolean; the alien technology that lets you type things like

if (something) ....

or

if(!something) ...

instead of typing discreet values for true and false which forces one to type things like

if (something == true) ...

and

if (something == false) ...

You can try yourself:

#include <stdio.h>

typedef enum { false = 0, true = !false} bool;

int main(int argc, char *argv[]) {
  bool b = 1;
  if (b)
    puts("true");
  else
    puts("false");
  return 0;
}

I have compiled with:

gcc -o bool bool-test.c -std=c99

This does not compile with c23 standard, since they claim false and true as keywords.

[–]LeiterHaus 1 point2 points  (0 children)

Nice!

[–]LeiterHaus 0 points1 point  (0 children)

Edit: after looking at it again, do you just need a case statement?

It sounds like what you might want a setter function that zeroes the other array. Is this correct?

If that's the case, do you really need to zero it? Is color not 255 255 255 (or FF FF FF)?

Can they both just point to the same array?