从静态 const 结构返回 const char **
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;
}
}
从静态初始化的静态 const 结构返回 const char ** 是否存在上下文或范围问题?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当然不存在范围问题。范围涉及变量,而不是值。 (不过,您的代码中缺少
{
会出现问题。)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.