Why define \0 as the first element of a char array in C?

CArrays

C 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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser1923105View Question on Stackoverflow
Solution 1 - CSergioView Answer on Stackoverflow