When {0}
is used to initialize an object, what does it mean? I can\'t find any references to {0}
anywhere, and because of the curly braces Google s
I also use it to initialize strings eg.
char mytext[100] = {0};
It's been awhile since I worked in c/c++ but IIRC, the same shortcut can be used for arrays as well.
{0} is an anonymous array containing its element as 0.
This is used to initialize one or all elements of array with 0.
e.g. int arr[8] = {0};
In this case all the elements of arr will be initialized as 0.
{0}
is a valid initializer for any (complete object) type, in both C and C++. It's a common idiom used to initialize an object to zero (read on to see what that means).
For scalar types (arithmetic and pointer types), the braces are unnecessary, but they're explicitly allowed. Quoting the N1570 draft of the ISO C standard, section 6.7.9:
The initializer for a scalar shall be a single expression, optionally enclosed in braces.
It initializes the object to zero (0
for integers, 0.0
for floating-point, a null pointer for pointers).
For non-scalar types (structures, arrays, unions), {0}
specifies that the first element of the object is initialized to zero. For structures containing structures, arrays of structures, and so on, this is applied recursively, so the first scalar element is set to the zero, as appropriate for the type. As in any initializer, any elements not specified are set to zero.
Intermediate braces ({
, }
) may be omitted; for example both these are valid and equivalent:
int arr[2][2] = { { 1, 2 }, {3, 4} };
int arr[2][2] = { 1, 2, 3, 4 };
which is why you don't have to write, for example, { { 0 } }
for a type whose first element is non-scalar.
So this:
some_type obj = { 0 };
is a shorthand way of initializing obj
to zero, meaning that each scalar sub-object of obj
is set to 0
if it's an integer, 0.0
if it's floating-point, or a null pointer if it's a pointer.
The rules are similar for C++.
In your particular case, since you're assigning values to sexi.cbSize
and so forth, it's clear that SHELLEXECUTEINFO
is a struct or class type (or possibly a union, but probably not), so not all of this applies, but as I said { 0 }
is a common idiom that can be used in more general situations.
This is not (necessarily) equivalent to using memset
to set the object's representation to all-bits-zero. Neither floating-point 0.0
nor a null pointer is necessarily represented as all-bits-zero, and a { 0 }
initializer doesn't necessarily set padding bytes to any particular value. On most systems, though, it's likely to have the same effect.
One thing to be aware of is that this technique will not set padding bytes to zero. For example:
struct foo
{
char c;
int i;
};
foo a = {0};
Is not the same as:
foo a;
memset(&a,0,sizeof(a));
In the first case, pad bytes between c and i are uninitialized. Why would you care? Well, if you're saving this data to disk or sending it over a network or whatever, you could have a security issue.
It's syntatic sugar to initialize your entire structure to empty/zero/null values.
Long version
SHELLEXECUTEINFO sexi;
sexi.cbSize = 0;
sexi.fMask = 0;
sexi.hwnd = NULL;
sexi.lpVerb = NULL;
sexi.lpFile = NULL;
sexi.lpParameters = NULL;
sexi.lpDirectory = NULL;
sexi.nShow = nShow;
sexi.hInstApp = 0;
sexi.lpIDList = NULL;
sexi.lpClass = NULL;
sexi.hkeyClass = 0;
sexi.dwHotKey = 0;
sexi.hMonitor = 0;
sexi.hProcess = 0;
Short version
SHELLEXECUTEINFO sexi = {0};
Wasn't that much easier?
It's also nice because: