为什么恒定初始化需要静态char *而不是静态char ** [英] Why is constant initialization need for static char* but not static char**

查看:88
本文介绍了为什么恒定初始化需要静态char *而不是静态char **的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以解释一下为什么这样的代码...

Can someone please explain why this code...

// main.c

#include <stddef.h>

static const int    g_a   = 1;
static const char*  g_b   = "hello";
static const char*  g_c[] = { "a",    "b",    NULL };

typedef struct Foo
{
  int           a;
  const char*   b;
  const char**  c;
} Foo;

static Foo f[] =
{
  { g_a,
    g_b,
    g_c }
};

int main( int argc, char* argv[] )
{
  return 0;
}

...产生此错误:

> gcc --version && gcc -g main.c 
gcc (GCC) 8.2.1 20181215 (Red Hat 8.2.1-6)
Copyright (C) 2018 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

main.c:19:5: error: initializer element is not constant
     g_b,
     ^~~
main.c:19:5: note: (near initialization for 'f[0].b')

我了解编译器正在要求常数来初始化 f [0] .b ,因此以下初始化是解决编译错误的方法:

I understand that the compiler is asking for a constant to initialize f[0].b, so the following initialization is a solution to the compile error:

static Foo f[] = { { g_a, "hello", g_c } };

但是为什么编译器在初始化以下内容时不发出类似的 constant required错误 f [0] .c (或者就此而言, f [0] .a ?)为什么这只是 f [0] .b 的问题?

But why does the compiler not issue a similar "constant required" error for the initialization of f[0].c? (Or, for that matter, f[0].a?) Why is this only an issue for f[0].b?

推荐答案

f [0] .a 类型为非恒定 int ,并由类型为 const int g_a 值初始化,该值在运行时无法更改,并且在编译时已知。因此,这里没有错误。

f[0].a has type "non-constant int" and is initialized by the value of g_a that is of type "const int" which cannot change at run time and is known at compile time. So no error here.

f [0] .b 具有类型指向<$ c的非恒定指针$ c> const char ,并应使用 g_b 的值进行初始化,该值也是 const char 。即使它具有初始化程序 g_b 可以在运行时更改,并且IIRC的初始化顺序也不确定。因此它的值在编译时未知,因此会出错。

f[0].b has type "non-constant pointer to const char" and should be initialized by the value of g_b that is also a "non-constant pointer to const char". Even if it has an initializer g_b can change at run time, and IIRC the initialization sequence is not determined. So its value is not known at compile time, hence the error.

f [0] .c 具有类型指向 const char 的非恒定指针,并由 g_c 初始化一个数组类型为 << c $ c> const char 的非恒定指针的元素的数组。数组的符号可以用作在编译时已知的常量指针。因此,这里没有错误。

f[0].c has type "non-constant pointer to non-constant pointer(s) to const char" and is initialized by g_c that is an array of elements of type "non-constant pointer to const char". The symbol of an array can be used as a constant pointer which is known at compile time. So no error here.

我想这就是您所缺少的:如果您希望将指针设为 const ,将修饰符放在指针的 * 后面,而不是指向的值,如下所示: [const ] char * const指针

This is what you are missing, I think: If you want a pointer to be const, place the modifier at the pointer, behind the *, not at the pointed value, like this: [const] char * const pointer.

这篇关于为什么恒定初始化需要静态char *而不是静态char **的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆