I recently came across the following code:
我最近遇到了以下代码:
static const struct gaih gaih[] = {
#if defined __UCLIBC_HAS_IPV6__
{ PF_INET6, gaih_inet },
#endif
{ PF_INET, gaih_inet },
#if 0
{ PF_LOCAL, gaih_local },
#endif
{ PF_UNSPEC, NULL }
};
struct gaih {
int family;
int (*gaih)(const char *name, const struct gaih_service *service,
const struct addrinfo *req, struct addrinfo **pai);
};
int func{
const struct gaih *g = gaih;
}
I understand the meaning of constant and static . But I able to decipher the logic behind static initialization of the constant object in such a crude way. please clarify the reason or use of doing it this way
我理解常量和静态的含义。但我能够以如此粗暴的方式破译常量对象静态初始化背后的逻辑。请澄清这样做的原因或用法
2 个解决方案
#1
1
file.c
seems to be a C file. Using static
in C has two meanings:
file.c似乎是一个C文件。在C中使用static有两个含义:
- Not on the stack (e.g. for variables inside functions that shall keep their value across several calls of the function)
- 不在堆栈上(例如,对于函数内部的变量,它们应该在函数的多次调用中保持它们的值)
- Not exported from this module (for variables that shall not be provided as a symbol to the linker)
- 不从此模块导出(对于不应作为符号提供给链接器的变量)
In this case it seems to be the second one. The variable gaih
shall not be exported (visible for the linker) static
and it shall not be changed const
. There is nothing crude.
在这种情况下,它似乎是第二个。变量gaih不应导出(对于链接器可见)静态,并且不应更改为const。没有什么粗糙的。
But to clarify further details the complete valid code would be needed. It seems to be a constant and static initialisation of an array of structs with just one entry. The variable g
is just a pointer to this single entry.
但为了澄清更多细节,将需要完整的有效代码。它似乎只是一个条目的结构数组的恒定和静态初始化。变量g只是指向此单个条目的指针。
This example has some similarity with the struct gaih_addrtuple
in nss.h
which is a linked list of host names and IP addresses used for gethostbyname
.
此示例与nss.h中的struct gaih_addrtuple有一些相似之处,nss.h是用于gethostbyname的主机名和IP地址的链接列表。
#2
1
The global variable gaih
is defined as static
, which means it is only visible in the current file, and const
, which means it can't be modified once initialized.
全局变量gaih定义为static,这意味着它只在当前文件和const中可见,这意味着它一旦初始化就无法修改。
The local variable g
is also defined as const
, meaning it can't be changed. It is initialized with the address of the global gaih
array, so g
can be treated as an array in this context.
局部变量g也定义为const,意味着它不能被改变。它使用全局gaih数组的地址初始化,因此在此上下文中可以将g视为数组。
g
can also be passed to another function, possibly in a different file. This allows the contents of gaih
to be read outside of the current file which would not be allowed by attempting to reference gaih
directly.
g也可以传递给另一个函数,可能在另一个文件中。这允许在当前文件之外读取gaih的内容,这是通过尝试直接引用gaih而不允许的。
#1
1
file.c
seems to be a C file. Using static
in C has two meanings:
file.c似乎是一个C文件。在C中使用static有两个含义:
- Not on the stack (e.g. for variables inside functions that shall keep their value across several calls of the function)
- 不在堆栈上(例如,对于函数内部的变量,它们应该在函数的多次调用中保持它们的值)
- Not exported from this module (for variables that shall not be provided as a symbol to the linker)
- 不从此模块导出(对于不应作为符号提供给链接器的变量)
In this case it seems to be the second one. The variable gaih
shall not be exported (visible for the linker) static
and it shall not be changed const
. There is nothing crude.
在这种情况下,它似乎是第二个。变量gaih不应导出(对于链接器可见)静态,并且不应更改为const。没有什么粗糙的。
But to clarify further details the complete valid code would be needed. It seems to be a constant and static initialisation of an array of structs with just one entry. The variable g
is just a pointer to this single entry.
但为了澄清更多细节,将需要完整的有效代码。它似乎只是一个条目的结构数组的恒定和静态初始化。变量g只是指向此单个条目的指针。
This example has some similarity with the struct gaih_addrtuple
in nss.h
which is a linked list of host names and IP addresses used for gethostbyname
.
此示例与nss.h中的struct gaih_addrtuple有一些相似之处,nss.h是用于gethostbyname的主机名和IP地址的链接列表。
#2
1
The global variable gaih
is defined as static
, which means it is only visible in the current file, and const
, which means it can't be modified once initialized.
全局变量gaih定义为static,这意味着它只在当前文件和const中可见,这意味着它一旦初始化就无法修改。
The local variable g
is also defined as const
, meaning it can't be changed. It is initialized with the address of the global gaih
array, so g
can be treated as an array in this context.
局部变量g也定义为const,意味着它不能被改变。它使用全局gaih数组的地址初始化,因此在此上下文中可以将g视为数组。
g
can also be passed to another function, possibly in a different file. This allows the contents of gaih
to be read outside of the current file which would not be allowed by attempting to reference gaih
directly.
g也可以传递给另一个函数,可能在另一个文件中。这允许在当前文件之外读取gaih的内容,这是通过尝试直接引用gaih而不允许的。