Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 294
Fausto Carvalho Marques Silva Avatar asked Jun 12 '26 10:06

Fausto Carvalho Marques Silva


1 Answers

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

like image 182
Fred Foo Avatar answered Jun 14 '26 00:06

Fred Foo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!