简体   繁体   English

从静态const结构返回一个const char **

[英]Returning a const char ** from a static const struct

a.cpp: 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? 从静态const结构静态返回const char **是否存在上下文或范围问题?

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. 这不可能是好事。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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