Use of "." for structure members

C

cman

I do not understand the use of a period to define structure members,
one sees this frequently in kernel code. Please explain. Here is the
structure:

static struct file_system_type ext2_fs_type = {
.owner = THIS_MODULE,
.name = "ext2",
.get_sb = ext2_get_sb,
.kill_sb = kill_block_super,
.fs_flags = FS_REQUIRES_DEV,
};

Tilak
 
B

Ben Pfaff

cman said:
I do not understand the use of a period to define structure members,
one sees this frequently in kernel code. Please explain.

It is a new feature in C99. Writing ".member = value"
initializes the named member to the given value. This syntax
allows members' initializers to be given in any order, it allows
explicitly initializing any subset of members, and it provides
documentation for the reader.
 
R

Richard Heathfield

cman said:
I do not understand the use of a period to define structure members,
one sees this frequently in kernel code. Please explain. Here is the
structure:

static struct file_system_type ext2_fs_type = {
.owner = THIS_MODULE,
.name = "ext2",
.get_sb = ext2_get_sb,
.kill_sb = kill_block_super,
.fs_flags = FS_REQUIRES_DEV,
};

This is not legal C90. In C99, it means "match each initialiser to the
member stated in the .x syntax". Here's a simpler example:

struct foo
{
int u;
int v;
int w;
int x;
int y;
int z;
};

struct foo bar = {
.v = 6;
.w = 17;
.z = 42;
};

At the end of this initialisation, bar.u, bar.x, and bar.y will have the
default static initialiser value of 0, and bar.v, bar.w, and bar.z will
have the values given in the code.

If you have a C99 compiler, you can use this syntax - but you probably
don't. Some compilers offer it as an extension, however, even though
they don't support C99. If your code must be portable to C90, do not
use this construct.
 
S

SM Ryan

# I do not understand the use of a period to define structure members,
# one sees this frequently in kernel code. Please explain. Here is the
# structure:

It doesn't define the structure members. The struct {...} defines
the structure members. This creates a struct value with possible
holes in it.

# static struct file_system_type ext2_fs_type = {
# .owner = THIS_MODULE,
# .name = "ext2",
# .get_sb = ext2_get_sb,
# .kill_sb = kill_block_super,
# .fs_flags = FS_REQUIRES_DEV,
# };
 

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

Members online

No members online now.

Forum statistics

Threads
474,434
Messages
2,571,689
Members
48,796
Latest member
Greg L.

Latest Threads

Top