一小段感觉不错的代码

[zz]tagged structure initialization

zhibo posted @ Nov 25, 2010 10:42:56 PM in Programming with tags c zz struct tagged initialization , 1217 readers

直接贴网址:http://stackoverflow.com/questions/3016107/what-is-tagged-structure-initialization-syntax/3017026#3017026


struct file_operations scull_fops = {
.owner = THIS_MODULE,
.llseek = scull_llseek,
.read = scull_read,
.write = scull_write,
.ioctl = scull_ioctl,
.open = scull_open,
.release = scull_release,
};

This declaration uses the standard C tagged structure initialization syntax.When you use aggregate initializers (initializers in {}) in the "traditional" ANSI C language (C89/90), you have to supply an individual initializer for each structure member in order, beginning with the first. For example

struct S { int a, b, c, d; };

struct S s = { 1, 2, 3, 4 };
/* 1 for `s.a`, 2 for `s.b` and so on... */

You are not required to specify initializers for all members, i.e. you can stop at any time (remaining members will be zero-initialized). If for some reason you only cared to explicitly initialize the third member of the structure, you had no other choice but to supply a "dummy" explicit initializer for the first and the second members (just to get to the desired third)

/* We only care to explicitly initialize `s.c` */
struct S s = { 0, 0, 3 };
/* and we have no choice but to explicitly initialize `s.a` and `s.b` as well */

The new specification of C language (C99) allows you to use "tagged" initializers by supplying the desired member name within the {}

struct S s = { .c = 3 };

That way you only explicitly initialize the desired member(s) (and ask the compiler to implicitly zero-initialize the rest).

This not only saves you some typing but also makes the aggregate initializers independent from the order in which the members are declared in the struct.

Aggregate initializers, as you probably know, can be used with arrays, too. And C99 supports "tagged" initialization with arrays as well. How the "tags" look in case of an array is illustrated by the following example

int a[10] = { [5] = 3 };

It is worth noting one more time that C language continues to stick to the "all or nothing" approach to aggregate initialization: if you specify an explicit initializer for just one (or some) members of a struct or an array, the whole aggregate (struct or array) gets initialized, and the members without explicit initializers get zero-initialized.

© Copyright 2011-2013 by Bo Zhi.

Content licensed under the Creative Commons attribution-noncommercial-sharealike License.

Contact me via mail or twitter.

AAA said:
Feb 08, 2022 03:44:19 PM

I am just genuinely thrilled to come across this great site along with does get pleasure from looking at valuable content put up below. Your concepts in the publisher ended up being wonderful, cheers to the talk about. 안전공원

AAA said:
Apr 19, 2022 06:09:35 PM

The a truly great place That i experienced owing to publish it all. The chances of what precisely I want to observe pray during near future you can expect to keep going meant for posting an extremely good place. North American Bancard Agent Program

AAA said:
May 27, 2022 08:59:01 PM I would like to thnkx for the efforts you have put in writing this blog. I’m hoping the same high-grade website post from you in the upcoming also. Actually your creative writing skills has inspired me to get my own site now. Actually the blogging is spreading its wings rapidly. Your write up is a good example of it. 먹튀검증
AAA said:
Jun 04, 2022 10:42:47 PM

This website is my inhalation, really fantastic layout and Perfect written content. 麥克風

AAA said:
Jun 13, 2022 10:19:34 PM

I’m impressed, I must say. Actually not often do I encounter a blog that’s both educative and entertaining, and let me inform you, you’ve hit the nail on the head. Your idea is excellent; the issue is something that not enough people are talking intelligently about. I’m very completely satisfied that I stumbled across this in my search for one thing relating to this. 回收手提電腦

meidir said:
Oct 03, 2022 07:53:04 PM

I will right away grab your rss feed to remain up to date on any succeeding articles you might write 手提電腦

meidir said:
Oct 25, 2022 05:44:48 PM

I visited a lot of website but I believe this one has something extra in it in it Passive Crypto Income

meidir said:
Oct 25, 2022 08:34:53 PM

Just have to thank you for this great information. This is what I was looking for (Thanks Bing Go here

meidir said:
Oct 25, 2022 09:22:05 PM

When visiting blogs, i usually discover a great content like yours How to Get Google AdSense Approval

meidir said:
Nov 05, 2022 12:54:10 AM

Your current blogs usually have got a lot of really up to date info. Where do you come up with this? Just saying you are very inspiring. Thanks again 바카라사이트

meidir said:
Nov 05, 2022 09:06:09 PM

Hello! Quick question that’s completely off topic. Do you know how to make your site mobile friendly? My weblog looks weird when viewing from my iphone. I’m trying to find a template or plugin that might be able to resolve this problem. If you have any recommendations, please share. Many thanks! Grand Wailea Resort Maui photographers

meidir said:
Nov 11, 2022 03:47:10 AM

Regards for all your efforts that you have put in this. Very interesting info. Lasik treatment in Dubai

meidir said:
Nov 13, 2022 03:39:48 PM

Merely wanna input that you have a very decent site, I like the style and design it really stands out. Crypto news

 

===============

 

Thanks for sharing, it is a great inspirational thread to will motivate many people to work on new things. Buy Formuler Z10 Pro Max

meidir said:
Nov 15, 2022 03:00:36 AM

There are some fascinating points with time here but I do not know if all of them center to heart. You can find some validity but I am going to take hold opinion until I consider it further. Very good post , thanks so we want much more! Added to FeedBurner as well 金禾娛樂城

 

================

 

Your weblog seems astounding – complete with high quality content articles.. and so forth. I feel you’ll get even superior good results with including some far more movies and pictures. What do you feel. Even though I’ve bookmarked it. Thank you. Primary English tuition Singapore

 

==================

 

thank for sharing this with all of us. Of course, what a great site and informative posts, I will bookmark this site. keep doing your great job and always gain my support. cheers for sharing this beautiful story 수원가라오케

meidir said:
Nov 16, 2022 04:59:11 PM

Youre so cool! I dont suppose Ive read anything such as this prior to. So nice to uncover somebody with many original ideas on this subject. realy thanks for starting this up. this fabulous website can be something that is required on the internet, somebody with a little originality. valuable problem for bringing something totally new on the web! Home remodeling Spokane

===========

Aw, this became an extremely nice post. In concept I have to devote writing like that additionally – taking time and actual effort to generate a great article… but so what can I say… I procrastinate alot through no indicates often go completed. Spokane home remodeling contractors

===========

hi there and thank you for your info – I have certainly picked up something new from right here. I did however expertise a few technical points using this site, since I experienced to reload the website lots of times previous to I could get it to load properly. I had been wondering if your web hosting is OK? Not that I’m complaining, but sluggish loading instances times will often affect your placement in google and could damage your quality score if advertising and marketing with Adwords. Anyway I’m adding this RSS to my e-mail and could look out for much more of your respective intriguing content. Ensure that you update this again soon.. Spokane Bathroom Remodel

===========

I discovered your site web site online and appearance several of your early posts. Keep the really good operate. I just now extra encourage Feed to my MSN News Reader. Seeking toward reading far more from you finding out down the line!… bathroom remodel spokane

===========

Thanks for the good writeup. It in reality used to be a enjoyment account it. Glance complex to more introduced agreeable from you! However, how can we communicate? spokane bathroom remodel

===========

Some truly interesting points you have written. Aided me a lot, just what I was looking for : D. Basement remodeling Spokane wa

===========

I discovered your site site on yahoo and check a few of your early posts. Keep on the good operate. I simply additional encourage Rss to my MSN News Reader. Seeking toward reading much more from you finding out down the road!… Basement remodeling Spokane

===========

I was reading through some of your blog posts on this site and I think this web site is really instructive! Keep on putting up. Does God forgive all sins

===========

I think this is one of the most vital information for me. And i am glad reading your article. But wanna remark on few general things, The website style is perfect, the articles is really great. Good job, cheers Bounce house

===========

I image this might be various upon the written content material? then again I still believe that it may be appropriate for nearly any form of subject subject matter, because it will continuously be gratifying to resolve a heat and delightful face or possibly pay attention a voice whilst preliminary landing. School of ministry

===========

Ha ha… I was just browsing around and took a glimpse at these responses. I can’t believe that there’s still this much attention. Thanks for posting about this. Restoration companies Spokane wa

===========

I like the efforts you have put in this, thank you for all the great blog posts. Water and fire damage restoration Spokane

meidir said:
Nov 17, 2022 08:37:56 PM You are so cool man, the post on your blogs are super great تمويل شخصي حتى لو عليك قرض
meidir said:
Nov 18, 2022 05:30:49 AM

Hey, I am ranking my site higher “pre spun articles”. safety deposit boxes for sale

 

============

 

After examine a number of of the blog posts in your web site now, and I truly like your method of blogging. I bookmarked it to my bookmark web site checklist and will probably be checking again soon. Pls take a look at my website as properly and let me know what you think. modular vault systems

 

===================

 

If at all possible, when you gain knowledge, can you mind updating your site with more information? It is rather ideal for me. vault door for sale

meidir said:
Nov 19, 2022 04:12:27 AM

Hey, you used to write magnificent, but the last several posts have been kinda boring… I miss your tremendous writings. Past several posts are just a little out of track! come on! 온라인바카라


Login *


loading captcha image...
(type the code from the image)
or Ctrl+Enter