Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

syntax of structure definition not clear

syntax

  • Please log in to reply
7 replies to this topic

#1 onus

onus

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 113 posts

Posted 25 September 2010 - 06:13 AM

I see at some places in Linux Kernel structures are defined like this
struct file_operations kt_fops = {
  read: kt_read,
  write: kt_write,
  open: kt_open,
  release: kt_release
};
I am not clear with what does read write or what type of synatx is read: write:
can some one point me to any link.
  • 0

#2 dbug

dbug

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 148 posts

Posted 25 September 2010 - 06:56 AM

read and write (like the other fields of this structure) are pointers to functions that implement the read and write operation for some device (disk, console, ...).

The syntax read: kt_read inside a structure variable declaration is a shortcut to avoid having to specify many NULL values. It specifies the name of the field you are initializing and its value. The fields not specified are initialized with 0 (NULL if they are pointers).

For example, with this structure:

struct test
{
    int field1;
    int field2;
    int filed3;
    int filed4;
};
the following initializations are equivalent:

struct text var1 =
{
    0,
    60,
    12,
    0
};
struct text var1 =
{
    field2: 60,
    field3: 12
};
As an additional benefit, this way it's easier to read the source code and you can avoid errors if for some reason the number or order of the fields of the structure change.
  • 0

#3 onus

onus

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 113 posts

Posted 25 September 2010 - 10:10 PM

Thanks a lot for your help.I was unable to find this in the C books I have with me,if you have any link online reference etc just pass it on here I came across
The Linux Kernel Module Programming Guide where they talk about it but your method is more clear to me than the link I gave.
  • 0

#4 dbug

dbug

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 148 posts

Posted 26 September 2010 - 12:20 AM

Here there is a description of some interesting methods to initialize structures and arrays in gcc.
  • -1

#5 onus

onus

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 113 posts

Posted 26 September 2010 - 03:57 AM

Thanks for the link.
  • 0

#6 onus

onus

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 113 posts

Posted 26 September 2010 - 04:04 AM

Can a function be a part of structure.
I came across a structure
here
LXR / The Linux Cross Reference
struct file_operations {
        struct module *owner;
        loff_t (*llseek) (struct file *, loff_t, int);
        ssize_t (*read) (struct file *, char __user *, size_t, loff_t *);
        ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *);
        ssize_t (*aio_read) (struct kiocb *, const struct iovec *, unsigned long, loff_t);
        ssize_t (*aio_write) (struct kiocb *, const struct iovec *, unsigned long, loff_t);
        int (*readdir) (struct file *, void *, filldir_t);
        unsigned int (*poll) (struct file *, struct poll_table_struct *);
        long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long);
        long (*compat_ioctl) (struct file *, unsigned int, unsigned long);
        int (*mmap) (struct file *, struct vm_area_struct *);
        int (*open) (struct inode *, struct file *);
        int (*flush) (struct file *, fl_owner_t id);
        int (*release) (struct inode *, struct file *);
        int (*fsync) (struct file *, int datasync);
        int (*aio_fsync) (struct kiocb *, int datasync);
        int (*fasync) (int, struct file *, int);
        int (*lock) (struct file *, int, struct file_lock *);
        ssize_t (*sendpage) (struct file *, struct page *, int, size_t, loff_t *, int);
        unsigned long (*get_unmapped_area)(struct file *, unsigned long, unsigned long, unsigned long, unsigned long);
        int (*check_flags)(int);
        int (*flock) (struct file *, int, struct file_lock *);
        ssize_t (*splice_write)(struct pipe_inode_info *, struct file *, loff_t *, size_t, unsigned int);
        ssize_t (*splice_read)(struct file *, loff_t *, struct pipe_inode_info *, size_t, unsigned int);
        int (*setlease)(struct file *, long, struct file_lock **);
};
are the above members declared in the structures pointer to functions?
  • 0

#7 dbug

dbug

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 148 posts

Posted 26 September 2010 - 11:19 AM

Except the first field (owner) all other fields are function pointers.
  • 0

#8 onus

onus

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 113 posts

Posted 26 September 2010 - 11:29 AM

Ok.
  • 0





Also tagged with one or more of these keywords: syntax

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download