Thread: struct member name prefixing

  1. #1
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808

    struct member name prefixing

    What is the purpose of prefixing struct member names like this?
    Code:
    struct sockaddr {
        unsigned short   sa_family;
        char             sa_data[14];
    };
    What is the point of the "sa_" ?

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868

    What is the point of the "sa_" ?
    A reminder to the programmer - sa == sock address, pehaps?

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I believe that in in K&R C [or some early implementations of C] struct types share the same "namespace", so in this old C standard, each struct member HAS to have a unique name - using a two-letter combination from the struct name itself helps making the names unique.

    This is completely obsolete these days, but for compatibility with really old compilers, it's still used in some places [plus of course if you change it now, you'd break all the old code that was written using the old style names - although some clever tricks with #defines may be able to "fix" that].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    A legacy from an ancient communal struct member namespace.
    That sounds sad but true.

    I thought it might have had something to do with debugging,
    that the member names are visible at some point where
    the struct type name (or the actual variable name) is not.
    But I haven't used debuggers very often.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A struct member as a pointer to a variable
    By mc61 in forum C Programming
    Replies: 5
    Last Post: 01-27-2008, 08:40 AM
  2. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  3. struct member parse error
    By bazzano in forum C Programming
    Replies: 3
    Last Post: 05-08-2006, 08:50 PM
  4. memory allocation for flexible array member of struct
    By jcarouth in forum C Programming
    Replies: 3
    Last Post: 09-11-2005, 12:19 PM
  5. accessing member in struct = segmentation
    By subflood in forum C Programming
    Replies: 5
    Last Post: 11-16-2004, 05:47 PM