简体   繁体   English

C语言中的全局变量结构声明

[英]Global variable declaration of structure in C

Can you give me the meaning of this code in C language? 您能用C语言给我这个代码的含义吗?

This is the global variable definition. 这是全局变量定义。

 static const struct_name global_variable_name = // (Added equals to here)
 {
   function_call_1, // (comma instead of semicolon)
   function_call_2, // ditto
   NULL,
   NULL,
 }

If almost looks like something trying to initialise a struct (but the equals is missing and the semicolons should be commas). 如果几乎看起来像是试图初始化一个结构的东西(但是等式不见了,分号应该是逗号)。 Where does this code come from and does it compile in its environment? 该代码从何而来,并在其环境中进行编译?

Given a struct (I am guessing about what kind of struct it is): 给定一个结构(我正在猜测它是哪种结构):

struct struct_name
{
    void* func1;
    void* func2;
    void* something1;
    void* something2;
};

And some function pointers: 还有一些函数指针:

void* func1 = NULL;
void* func2 = NULL;

Then your code with = to initialize and commas for the args: 然后,用=初始化args并用逗号隔开的代码:

static const  struct_name  global_variable_name = 
{
   func1,
   func2,
   NULL,
   NULL,
};

Just a guess, as the code that you provided does not compile and is not valid. 只是一个猜测,因为您提供的代码无法编译且无效。 It looks like it might be some #define trickery, are either struct_name or global_variable_name #defines? 看起来可能是一些#define struct_name ,是struct_name还是global_variable_name #defines?

Update 更新

Based on your latest edit and your comment: 根据您的最新编辑和评论:

yes you are right, I modified the code. 是的,您是对的,我修改了代码。 This is the global variable definition. 这是全局变量定义。

Then, what is happening is that the code is initialising a global variable global_variable_name of type struct_name which is a struct. 然后,发生的事情是该代码正在初始化类型为struct_name的全局变量global_variable_name In the same way that you can initialize an integer when you declare it as follows: 以与可以在声明整数时初始化整数的方式相同,如下所示:

int myInteger = 1;

You can initialize a struct when you declare it. 您可以在声明结构时对其进行初始化。 Let's take a simpler case: 让我们来看一个简单的例子:

struct simple_struct
{
    int val1;
    int val2;
};

static const  simple_struct  global_simpleStruct = 
 {
   1,
   2
 };

int _tmain(int argc, _TCHAR* argv[])
{
    std::cout << "val1:" << global_simpleStruct.val1 << ", val2:" << global_simpleStruct.val2;
    return 0;
}

The output will be: val1:1, val2:2 输出将是: val1:1, val2:2

See the information here and search for 'struct initialization'. 此处查看信息并搜索“结构初始化”。

Hopefully this makes it clearer. 希望这可以使它更清晰。

It means nothing, it is gibberish and will not compile on a C compiler. 它没有任何意义,毫无意义,无法在C编译器上进行编译。

Most likely somebody was trying to initialize a struct but failed. 很可能有人试图初始化一个结构但失败了。

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

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