新闻  |   论坛  |   博客  |   在线研讨会
[转载]C语言struct的一种初始化方法
jobs | 2012-10-20 23:30:18    阅读:4655   发布文章

今天在翻看 Linux 下C语言编程时看到这篇文章,觉得非常不错,于是与EEPW论坛分享。

说实话还是第一次发现这种struct初始化方法,但在linux的内核代码中几乎都是用这种方法的


typedef struct str
{
int a;
int b;
}Str;

int main()
{
Str s={a:1,b:2};//注意这里的冒号
printf("a=%d/nb=%d/n",s.a,s.b);
}



原文是在linux内核2.4.18的linux/fs/ext2/super.c文件里的,如下:
static struct super_operations ext2_sops = {
read_inode: ext2_read_inode,
write_inode: ext2_write_inode,
put_inode: ext2_put_inode,
delete_inode:ext2_delete_inode,
put_super: ext2_put_super,
write_super: ext2_write_super,
statfs: ext2_statfs,
remount_fs: ext2_remount,
};


super_operations的定义在linux/include/linux/fs.h文件中,原文为:
struct super_operations {
void (*read_inode) (struct inode *);

/* reiserfs kludge. reiserfs needs 64 bits of information to
** find an inode. We are using the read_inode2 call to get
** that information. We don't like this, and are waiting on some
** VFS changes for the real solution.
** iget4 calls read_inode2, iff it is defined
*/
void (*read_inode2) (struct inode *, void *) ;
void (*dirty_inode) (struct inode *);
void (*write_inode) (struct inode *, int);
void (*put_inode) (struct inode *);
void (*delete_inode) (struct inode *);
void (*put_super) (struct super_block *);
void (*write_super) (struct super_block *);
void (*write_super_lockfs) (struct super_block *);
void (*unlockfs) (struct super_block *);
int (*statfs) (struct super_block *, struct statfs *);
int (*remount_fs) (struct super_block *, int *, char *);
void (*clear_inode) (struct inode *);
void (*umount_begin) (struct super_block *);

/* Following are for knfsd to interact with "interesting" filesystems
* Currently just reiserfs, but possibly FAT and others later
*
* fh_to_dentry is given a filehandle fragement with length, and a type flag
* and must return a dentry for the referenced object or, if "parent" is
* set, a dentry for the parent of the object.
* If a dentry cannot be found, a "root" dentry should be created and
* flaged as DCACHE_NFSD_DISCONNECTED. nfsd_iget is an example implementation.
*
* dentry_to_fh is given a dentry and must generate the filesys specific
* part of the file handle. Available length is passed in *lenp and used
* length should be returned therein.
* If need_parent is set, then dentry_to_fh should encode sufficient information
* to find the (current) parent.
* dentry_to_fh should return a 1byte "type" which will be passed back in
* the fhtype arguement to fh_to_dentry. Type of 0 is reserved.
* If filesystem was exportable before the introduction of fh_to_dentry,
* types 1 and 2 should be used is that same way as the generic code.
* Type 255 means error.
*
* Lengths are in units of 4bytes, not bytes.
*/
struct dentry * (*fh_to_dentry)(struct super_block *sb, __u32 *fh, int len, int fhtype, int parent);
int (*dentry_to_fh)(struct dentry *, __u32 *fh, int *lenp, int need_parent);
int (*show_options)(struct seq_file *, struct vfsmount *);
};


转载自:http://blog.csdn.net.sixxs.org/cacf10/article/details/591601

*博客内容为网友个人发布,仅代表博主个人观点,如有侵权请联系工作人员删除。

参与讨论
登录后参与讨论
推荐文章
最近访客