Why define \0 as the first element of a char array in C?
CArraysC Problem Overview
When I read BlueZ source code, I often see char
arrays defined like this:
// bluez/android/sco-msg.h
static const char BLUEZ_SCO_SK_PATH[] = "\0bluez_sco_socket";
What good is it to define the first element as \0
?
C Solutions
Solution 1 - C
In your particular case this array is used as pathname for a PF_LOCAL
socket; see here. And leading NUL is used to point that address is an abstract one. From man 7 unix:
> an abstract socket address is distinguished by the fact that > sun_path[0] is a null byte ('\0').
And this is the only reason why the first element is \0
.