Strange sintax

  • Follow


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
0
Reply inuY4sha1 (12) 4/2/2008 8:58:16 AM

In article <9906693a-e3ed-468e-83af-e0d045c71e43@b64g2000hsa.googlegroups.com>,
InuY4sha  <inuY4sha@email.it> wrote:
>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.
-- 
  "MAMA: Oh--So now it's life. Money is life. Once upon a time freedom
   used to be life--now it's money. I guess the world really do change.
   WALTER: No--it was always money, Mama. We just didn't know about it."
                                              -- Lorraine Hansberry
0
Reply roberson2 (8067) 4/2/2008 9:09:18 AM


InuY4sha wrote:

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

-- 
Hi, I'm a .signature virus, please copy/paste me to help me spread
all over the world.
0
Reply xiyou.wangcong (255) 4/2/2008 9:14:30 AM

Walter Roberson wrote:

> In article
> <9906693a-e3ed-468e-83af-e0d045c71e43@b64g2000hsa.googlegroups.com>,
> InuY4sha  <inuY4sha@email.it> wrote:
>>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 .

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

-- 
Hi, I'm a .signature virus, please copy/paste me to help me spread
all over the world.
0
Reply xiyou.wangcong (255) 4/2/2008 9:19:13 AM

roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson) writes:

> In article <9906693a-e3ed-468e-83af-e0d045c71e43@b64g2000hsa.googlegroups.com>,
> InuY4sha  <inuY4sha@email.it> wrote:
>>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 .

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.

-- 
Ben.
0
Reply ben.usenet (6515) 4/2/2008 9:24:09 AM

roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson) writes:

> C99 allows an extra comma at the end of an initialization list.

So does C89.
-- 
int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.\
 \n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof p-1;putchar(p[i]\
);}return 0;}
0
Reply blp (3953) 4/2/2008 4:06:41 PM

On Apr 2, 9:06=A0am, Ben Pfaff <b...@cs.stanford.edu> wrote:
> rober...@ibd.nrc-cnrc.gc.ca (Walter Roberson) writes:
> > C99 allows an extra comma at the end of an initialization list.
>
> So does C89.

There are some compilers that will blow a gasket, though (e.g. those
for OpenVMS).
0
Reply dcorbit (2696) 4/2/2008 7:57:24 PM

user923005 <dcorbit@connx.com> writes:

> On Apr 2, 9:06�am, Ben Pfaff <b...@cs.stanford.edu> wrote:
>> rober...@ibd.nrc-cnrc.gc.ca (Walter Roberson) writes:
>> > C99 allows an extra comma at the end of an initialization list.
>>
>> So does C89.
>
> 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.
-- 
"All code should be deliberately written for the purposes of instruction. 
 If your code isn't readable, it isn't finished yet."
--Richard Heathfield
0
Reply blp (3953) 4/2/2008 8:25:45 PM

"Ben Pfaff" <blp@cs.stanford.edu> wrote in message 
news:877ifg9f7a.fsf@blp.benpfaff.org...
> user923005 <dcorbit@connx.com> writes:
>
>> On Apr 2, 9:06 am, Ben Pfaff <b...@cs.stanford.edu> wrote:
>>> rober...@ibd.nrc-cnrc.gc.ca (Walter Roberson) writes:
>>> > C99 allows an extra comma at the end of an initialization list.
>>>
>>> So does C89.
>>
>> There are some compilers that will blow a gasket, though (e.g. those
>> for OpenVMS).
>
> Then they aren't C89 compilers.

True enough, but I have to use them.
;-)

> C99 did add the ability to add a trailing comma in one context:
> within the list of enumerated values in an enum definition.
> -- 
> "All code should be deliberately written for the purposes of instruction.
> If your code isn't readable, it isn't finished yet."
> --Richard Heathfield 



-- 
Posted via a free Usenet account from http://www.teranews.com

0
Reply dcorbit (2696) 4/2/2008 11:01:23 PM

8 Replies
24 Views

(page loaded in 0.415 seconds)

Similiar Articles:
















6/19/2012 6:27:54 AM


Reply: