Returning a const char ** from a static const struct

a.cpp:

static const struct A {
    int a1;
    const char ** a2;
} as[] = {
    {1,(const char *[]){"LOL",NULL}},
    {2,(const char *[]){"LOL","LOL2",NULL}}
};

const char ** getA(int a) {
    int i = 0;
    for(;i< sizeof(as)/sizeof(struct A);i++){
       if (as[i].a1 == a)
           return as[i].a2;
    }
}

Is there a context or scope problem in returning const char ** from a static const struct initialized statically?

Upvotes: 3

Views: 687

Answers (3)

caf
caf

Reputation: 239011

No, that is fine - compound literals that occur outside the body of a function have static storage duration.

Upvotes: 1

Mark Ransom
Mark Ransom

Reputation: 308121

You're trying to put a variable sized array of pointers into a fixed size struct. That can't be good.

Upvotes: 1

Fred Foo
Fred Foo

Reputation: 363517

There's certainly no scope problem. Scope pertains to variables, not to values. (There is a problem with missing { in your code, though.)

Upvotes: 2

Related Questions