Fagun
Fagun

Reputation: 23

Global variable declaration of structure in C

Can you give me the meaning of this code in C language?

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,
 }

Upvotes: 0

Views: 3035

Answers (2)

acarlon
acarlon

Reputation: 17272

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:

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?

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. 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

See the information here and search for 'struct initialization'.

Hopefully this makes it clearer.

Upvotes: 4

Lundin
Lundin

Reputation: 214810

It means nothing, it is gibberish and will not compile on a C compiler.

Most likely somebody was trying to initialize a struct but failed.

Upvotes: 1

Related Questions