Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 585 Vote(s) - 3.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
What is this C syntax (used in Linux drivers/net/bonding/bond_main.c)?

#1
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.)
Reply

#2
That is a <a href="https://gcc.gnu.org/onlinedocs/gcc/Designated-Inits.html">**designated initializer**</a>. It allows you to intialize the contents of the array in an arbitrary order.
Reply

#3
Here names is the array of char pointes.


These pointers are pointing to the string stored in read only section.

And Initilizing is done using **Designated Initializers** methods.

SO here you can initilize any array with any order of their index.
Those macros are index of array.
Reply

#4
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][1] for detail.

[1]:

[To see links please register here]

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through