Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prefixes in member names of a struct

Tags:

Consider

struct iovec {    ptr_t iov_base; /* Starting address */    size_t iov_len; /* Length in bytes */ }; 

or

struct stat { dev_t     st_dev;     /* ID of device containing file */ ino_t     st_ino;     /* inode number */ mode_t    st_mode;    /* protection */ nlink_t   st_nlink;   /* number of hard links */ uid_t     st_uid;     /* user ID of owner */ gid_t     st_gid;     /* group ID of owner */ dev_t     st_rdev;    /* device ID (if special file) */ off_t     st_size;    /* total size, in bytes */ blksize_t st_blksize; /* blocksize for file system I/O */ blkcnt_t  st_blocks;  /* number of 512B blocks allocated */ time_t    st_atime;   /* time of last access */ time_t    st_mtime;   /* time of last modification */ time_t    st_ctime;   /* time of last status change */ }; 

What's the point of prefixing those member names (iov_, st_)?

like image 997
PSkocik Avatar asked Mar 08 '16 17:03

PSkocik


People also ask

When referencing user string variable What prefix do you use?

From my experience, C++ code tends to use an underscore or m_ as a prefix for member variables.

What is the prefix of variable?

The idea of variable prefixesThey are used to tell the developer what kind of variable he is using. The first prefix for example is m_ and the conclusion is this variable is a member. When I learned programming we had to give the developer more information in case of a member.

Can two elements in a struct have the same name?

Yes, that is perfectly fine.


1 Answers

Once upon a very long time ago (way before even I started programming in C, which is over 30 years ago), the member names of structure elements had to be unique across all structures in a program — instead of 'per structure tag' as the requirement is now, and has been since the days of K&R — The C Programming Language 1st Edition, by Kernighan & Ritchie, 1978.

As a consequence, in those days, the prefix helped segregate the members of one structure type from every other structure type. Consequently, the habit grew of using such prefixes, and it continues with more recently developed structure types. The struct stat dates back to the days of the old system; struct iovec is a more recent invention (maybe sometime in the 1980s) which followed the old tradition.

Using prefixes does no harm. It could be marginally helpful. If you see gadzooks.st_mtime, you don't need to hunt the definition of gadzooks to guess with high confidence that it is of type struct stat.

Incidentally, you can find an early version of 'The C Reference Manual' as part of the Unix 7th Edition manuals (Unix Programmer's Manual Volume 2A) which says (§8.5 on p244, emphasis added):

The names of structure members and structure tags may be the same as ordinary variables, since a distinction can be made by context. However, names of tags and members must be distinct. The same member name can appear in different structures only if the two members are of the same type and if their origin with respect to their structure is the same; thus separate structures can share a common initial segment.

The same manual documents the long obsolete =+ family of assignment operators (in contrast to the modern, meaning since about 1976, += family of assignment operators).

like image 149
Jonathan Leffler Avatar answered Oct 28 '22 09:10

Jonathan Leffler