开发者

Returning a const char ** from a static const struct

开发者 https://www.devze.com 2023-02-24 16:45 出处:网络
a.cpp: static const struct A { int a1; const char ** a2; } as[] = { {1,(const char *[]){\"LOL\",NULL}开发者_如何学Python},

a.cpp:

static const struct A {
    int a1;
    const char ** a2;
} as[] = {
    {1,(const char *[]){"LOL",NULL}开发者_如何学Python},
    {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.)


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号