简体   繁体   中英

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?

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

不,这很好 - 在函数体外发生的复合文字具有静态存储持续时间。

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM