Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Initialize char array to hold non-null-terminated string [duplicate]

I am trying to initialize a char array with a long string. However, I do not want it to be NULL terminated.

This:

const char s[] = "The actual string is much longer then this...";

is much easier to read (and to write) than this:

const char s[] = {'T', 'h', 'e', ' ', 'a', 'c', 't', 'u', 'a', 'l', ' ', 's', ...};

but the former gets NULL terminated. Is there a way to avoid the NULL on a string literal?

The reason for doing this is that there is the need to pack densely strings in memory of fixed size length known during development.

like image 537
Baruch Avatar asked Oct 18 '25 21:10

Baruch


1 Answers

No.

A string literal is a C-string which, by definition, is null-terminated.

Either ignore the final character, revisit your requirements (why do you care about a final character?!) or … I dunno, something else. Perhaps generate the objects with xxd?

like image 158
Lightness Races in Orbit Avatar answered Oct 21 '25 11:10

Lightness Races in Orbit