Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is this C syntax (used in Linux drivers/net/bonding/bond_main.c)?

Tags:

c

syntax

linux

I've written plenty of C before, but I don't recognize this syntax:

static const char *names[] = {
    [BOND_MODE_ROUNDROBIN] = "load balancing (round-robin)",
    [BOND_MODE_ACTIVEBACKUP] = "fault-tolerance (active-backup)",
    [BOND_MODE_XOR] = "load balancing (xor)",
    [BOND_MODE_BROADCAST] = "fault-tolerance (broadcast)",
    [BOND_MODE_8023AD] = "IEEE 802.3ad Dynamic link aggregation",
    [BOND_MODE_TLB] = "transmit load balancing",
    [BOND_MODE_ALB] = "adaptive load balancing",
};

The [...] = part is what looks weird to me. (By the way, BOND_MODE_ROUNDROBIN and the others are macros which just expand to integers.)

like image 383
Alex D Avatar asked Aug 01 '14 06:08

Alex D


2 Answers

It's called designated initializers, which is introduced in C99. GCC also supports it as an extension.

It's used to initialize structures and arrays, see Designated Initializers for detail.

like image 158
Yu Hao Avatar answered Nov 15 '22 19:11

Yu Hao


That is a designated initializer. It allows you to intialize the contents of the array in an arbitrary order.

like image 4
Ignacio Vazquez-Abrams Avatar answered Nov 15 '22 19:11

Ignacio Vazquez-Abrams