Strange sintax

I

InuY4sha

Hi all :)
sorry.. I know this is a very mean question but I can't get the
following couple of lines:
static int dev_init(struct net_device *dev);
struct net_device my_device = { init: dev_init, };
To me it looks something like defining a function and creating a
structure that stores a label referencing that function.. what's the
scope behind this ?
Also I don't get the comma after "dev_init"...
Can somebody explain me this sintax please ?

Thanks in advance,
RM
 
W

Walter Roberson

sorry.. I know this is a very mean question but I can't get the
following couple of lines:
static int dev_init(struct net_device *dev);
struct net_device my_device = { init: dev_init, };
To me it looks something like defining a function and creating a
structure that stores a label referencing that function.. what's the
scope behind this ?

I suspect it is C99 and it means that the my_device field
named 'init' is to be initialized to the function pointer dev_init .
Also I don't get the comma after "dev_init"...
Can somebody explain me this sintax please ?

C99 allows an extra comma at the end of an initialization list.
It tends to make code easier to maintain.
 
W

WANG Cong

InuY4sha said:
Hi all :)
sorry.. I know this is a very mean question but I can't get the
following couple of lines:
static int dev_init(struct net_device *dev);
struct net_device my_device = { init: dev_init, };
To me it looks something like defining a function and creating a
structure that stores a label referencing that function.. what's the
scope behind this ?

It's not a label, it's a field name. But this is not standard, and also
deprecated by gcc. You should use the standard way:

struct net_device my_device = { .init = dev_init, };
Also I don't get the comma after "dev_init"...
Can somebody explain me this sintax please ?

Just like other initialization, the last comma doesn't matter here.
 
W

WANG Cong

Walter said:
I suspect it is C99 and it means that the my_device field
named 'init' is to be initialized to the function pointer dev_init .

No. C99 uses the ".identifier" form.

And yes, 'init' should be a member of the net_device struct, and a function
pointer of that type like dev_init().
 
B

Ben Bacarisse

I suspect it is C99 and it means that the my_device field
named 'init' is to be initialized to the function pointer dev_init .

The C99 syntax for a designated initializer is:

struct net_device my_device = { .init = dev_init, };

I think the "field_name:" form is a GCCism.
 
B

Ben Pfaff

user923005 said:
There are some compilers that will blow a gasket, though (e.g. those
for OpenVMS).

Then they aren't C89 compilers.

C99 did add the ability to add a trailing comma in one context:
within the list of enumerated values in an enum definition.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,121
Latest member
LowellMcGu
Top