1. Example 1
Case1
enum StatsampleType
{
ST_FIVE_SECONDS = 0,
ST_ONE_MINUTE,
ST_FIFTEEN_MINUTES,
ST_TWO_HOUR,
ST_ONE_DAY,
ST_LAST_LIMITED,
};
struct QueueInfo
{
unsigned ginval; /* interval time for gathering (seconds) */
int qcount; /* length of traffic queue */
};
static struct QueueInfo qinfo[] =
{
[ST_FIVE_SECONDS] = {SGI_FIVE_SECONDS, 0},
[ST_ONE_MINUTE] = {SGI_ONE_MINUTE, 0},
[ST_FIFTEEN_MINUTES] = {SGI_FIFTEEN_MINUTES, 0},
[ST_TWO_HOUR] = {SGI_TWO_HOUR, 0},
[ST_ONE_DAY] = {SGI_ONE_DAY, 0},
[ST_LAST_LIMITED] = {SGI_MAX_INTERVAL, 0},
};
Notice: You can initialize the unodered elements of the struture ( it means unlike ordered default).
Text Box
/*
* =====================================================================================
*
* Filename: test_struct.c
*
* Description:
*
* Version: 1.0
* Created: 01/24/2013 02:00:43 PM
* Revision: none
* Compiler: gcc
*
* Author: quyendv
* Company: dovanquyen.vn@gmail.com
*
* =====================================================================================
*/
#include <stdio.h>
struct queue
{
int a;
int b;
};
static struct queue qinfo[] =
{
[0] = {1, 2},
[2] = {-1, -2},
[1] = {3, 5},
[3] = {4,2},
};
int main()
{
printf("qinfo[1].a = %d\tqinfo[1].b = %d\n", qinfo[1].a, qinfo[1].b);
return 0;
}
Result: qinfo[3].a = 4 qinfo[3].b = 2