What is the need of empty braces '{ }' at the end of array of structs? What is the need of empty braces '{ }' at the end of array of structs? c c

What is the need of empty braces '{ }' at the end of array of structs?


This particular change was part of the sysctl net: Remove unused binary sysctl code commit by Eric W. Biederman, changing the initialization of the last element of the ip_ct_sysctl_table array from {0} to {} (and performs similar changes to many other array initializations).

The {0} pattern seems to have been around for much longer though, and both {0} or {} final element-initialization is commonly (in the Linux source code) explicitly referred to as Terminating entry, so it is likely a pattern present to allow consuming these arrays without knowing their lengths, terminating consumption when hitting the zero-initialized terminating entry. E.g. for the similar arrays in sound/aoa/fabrics/snd-aoa-fabric-layout.c the intent of the zero-initialization is even explicitly mentioned in a comment, e.g.:

static struct codec_connection toonie_connections[] = {  {      .connected = CC_SPEAKERS | CC_HEADPHONE,      .codec_bit = 0,  },  {} /* terminate array by .connected == 0 */};


You're probably familiar with zero-terminated strings. ctl_table ip_ct_sysctl_table[] is a zero-terminated array, i.e. the last array entry has all-zero members.


What is the need of empty braces '{ }' at the end of array of structs?

To be clear: the "empty braces '{ }' at the end of array of structs" is not needed to satisfy C syntax requirements.

When should I use empty braces at the end of an array of structs?

When code wants a sentinel value.

It is sometimes useful for the program to have a final array element of all zeros - certainly to detect the end. The need comes from the application's use of array ctl_table ip_ct_sysctl_table[], not from a C language need.