#initializer element is not constant

32 messages · Page 1 of 1 (latest)

glad compass
#

I'm getting this warning:

warning: initializer element is not constant [-Wpedantic]
 7 |     (struct Point){.is_source = true, .is_target = true},
note: (near initialization for ‘interval_1[0]’)

This is my code

#include <stdbool.h>

struct Point {
  bool is_target;
  bool is_source;
};

static struct Point interval_1[] = {
    (struct Point){.is_source = true, .is_target = true},
    (struct Point){.is_source = false, .is_target = false},

};

int main(){
  // whatever...
}

This is very confusing, How can I fix this? Note that this is an extremely simplified version of my code. interval_1 js meant to be a testcase for my unit tests.

I tried playing around with constants and macros but I can't figure it out.

verbal swallowBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

hardy fog
#

static/global variables need to be initialized with constant expressions
(){} is not a constant expression

static struct Point interval_1[] = {
    {.is_source = true, .is_target = true},
    {.is_source = false, .is_target = false},
};
glad compass
#

Hmmmm I see

The issue is that I actually have the following macros defined w the struct

#define EMPTY                                                                  \
  (struct Point) { .is_target = false, .is_source = false }
#define SOURCE                                                                 \
  (struct Point) { .is_target = false, .is_source = true }
#define TARGET                                                                 \
  (struct Point) { .is_target = true, .is_source = false }
#define BOTH                                                                   \
  (struct Point) { .is_target = true, .is_source = true }

And I used it both as compile time constants and as regular variables

#

Do I have separate them into constant and non-constant? Why doesn't (const struct Point){...} work in this case?

hardy fog
#

const doesnt make things a constant expression

#

theres constexpr but i dont think you can use it with compound literals

glad compass
#

Okay I see, constant expression means literal right?

hardy fog
#

also its for C23

hardy fog
hardy fog
glad compass
hardy fog
#

eg

void fn() {
    struct S s1 = {123, 123, 123}; // ok 
    struct S s2 = (struct S) {123, 123, 123}; // ok - not a constexpr
    
    s1 = {234, 234, 234}; // not ok
    s2 = (struct S) {234, 234, 234}; // ok
}
glad compass
#

Hmmmm I see

#

If it's in a function, is assigning to s1 still not ok?

hardy fog
#

yes

glad compass
#

Ahhh yeah I see, it's the same example as mine, we'd have to typecast the constexpr

hardy fog
#

;compile c -Wall -Wextra -Wpedantic

typedef struct {int x, y, z; } S;
int main() {
    S s = {123, 123, 123};
    s = (S){1, 2, 3};
    s = {1, 2, 3};
    (void)s;
}
```#
vapid ferryBOT
#
Compiler Output
<source>: In function 'main':
<source>:5:9: error: expected expression before '{' token
    5 |     s = {1, 2, 3};
      |         ^
Build failed
glad compass
#

Hmmm that's interesting, I wasn't aware of the diff between const and constexpr in C

#

Thank you

verbal swallowBOT
#

Thank you and let us know if you have any more questions!

This thread is now set to auto-hide after an hour of inactivity

glad compass
#

One more thing actually lol

#

Const expressions don't have a specific type right? Like there's no specific struct type attached to them

hardy fog
#

pretty much everything in C is bound to a type

#

its statically typed

#

or wdym

glad compass
hardy fog
#

yes

glad compass
#

Okkk I get it

hardy fog
#

its how the standard defines compound literals