More complex array & struct style.

This is a discussion on More complex array & struct style. within the C Programming forums, part of the General Programming Boards category; Hello guys, Can someone explain me what's going on in static const struct tlv_definition xyz_att_tlvdef? Code: //-------- .h file enum ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    37

    More complex array & struct style.

    Hello guys,

    Can someone explain me what's going on in
    static const struct tlv_definition xyz_att_tlvdef?


    Code:
    //-------- .h file
    enum tlv_type {
    	TLV_TYPE_FIXED,
    	TLV_TYPE_T,
    	TLV_TYPE_TV,
    	TLV_TYPE_TLV,
    	TLV_TYPE_TL16V,
    };
    
    struct tlv_def {
    	enum tlv_type type;
    	u_int8_t fixed_len;
    };
    
    //-------- .c file
    static const struct tlv_definition xyz_att_tlvdef = {
    	.def = {
    		[MOBILE_ID]	= { TLV_TYPE_TLV },
    		[NAME_LONG]	= { TLV_TYPE_TLV },
    		[NAME_SHORT]	= { TLV_TYPE_TLV },
    		[UTC]		= { TLV_TYPE_TV },
    		[NET_TIME_TZ]	= { TLV_TYPE_FIXED, 7 },
    		[LSA_IDENT]	= { TLV_TYPE_TLV },
    	},
    };
    I haven't seen this style of programming before, can someone provide me a link where such technique is explained? And what is the advantage of this style?
    Code:
    static const char *lm_cause_names[] = {
    	[CAUSE_NORMAL]			= "Normal event",
    	[CAUSE_ABNORMAL_UNSPEC]	= "Abnormal release, unspecified",
    	[CAUSE_ABNORMAL_UNACCT]	= "Abnormal release, channel unacceptable",
    	[CAUSE_ABNORMAL_TIMER]		= "Abnormal release, timer expired",
    	[CAUSE_ABNORMAL_NOACT]		= "Abnormal release, no activity on radio path",
    	[CAUSE_PREMPTIVE_REL]		= "Preemptive release",
    };
    Thank you.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    This is C99's named initialization. The .def field of the struct is initialized to what's given, which also uses named initialization, so presumably the .def field is an array of some sort. The advantage over a normal initializer is that you don't have to know what the values of those constants are, and any "gaps" in the sequence are automatically filled to 0 for you.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Just so that's clear, this is an instance of a single struct, not a struct definition:
    Code:
    static const struct tlv_definition xyz_att_tlvdef =
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Registered User
    Join Date
    Mar 2009
    Posts
    37
    Thank you for your replies,

    But can someone give me a link so I can study it carefully?
    Cause I'm a bit nervous when learning things only from examples. If someone put an article with some explanation what exactly is going on, than I know how such code behaves. I really feel satisfied when writing software that don't crash

    regards,
    Andaluz.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Converting from C to C++
    By Taka in forum C++ Programming
    Replies: 5
    Last Post: 04-08-2009, 03:16 AM
  2. Replies: 10
    Last Post: 05-19-2006, 12:23 AM
  3. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM
  4. Bi-Directional Linked Lists
    By Thantos in forum C Programming
    Replies: 6
    Last Post: 12-11-2003, 10:24 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 02:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21