4

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.

8
  • 4
    Why do you want to avoid the NULL-terminator? Commented Dec 1, 2015 at 11:48
  • 1
    We need a lot more information to give you a useful answer. For example, is this initialization only going to occur once in the program's lifetime? Is performance an issue? What's the outer problem? Commented Dec 1, 2015 at 12:05
  • It will actually be an array of strings, which I need to be packed tight into memory (no NULL between the strings) for various fast indexing purposes Commented Dec 1, 2015 at 12:06
  • How will the code that indexes the array know where the strings end? Commented Dec 1, 2015 at 12:07
  • 1
    Please add this constraint into the question. Commented Dec 1, 2015 at 12:16

5 Answers 5

4

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?

Sign up to request clarification or add additional context in comments.

Comments

2

I would do:

size_t length = 45;
char s[] = "The actual string is much longer then this..";
s[length - 1] = ".";

See what you have there has a trade-off between readability and functionality and I think that you can get away easily with this, since you can not avoid the NULL terminating string in the "normal" initialization.


If I were in your shoes, I would re-consider my approach and use std::string.

10 Comments

What is length? If you're going to do a strlen, that's an O(n) operation. An O(n) operation for initialization? This looks heavy.
That rings me a bell @LightnessRacesinOrbit, but if he wants to do that kind of stuff, he better know the length and hard-code it. Otherwise, use a NULL-terminating string.
Yes, I agree. The requirement itself is suspect.
@LightnessRacesinOrbit Do you have some reason to think performance matters here? This is just initialization. Also, the strlen could be optimized out here. Your complaint sounds like premature optimization -- the worst excuse to make a design more complex than it needs to be.
If he deletes the one above, I'll delete my one above and this one. Otherwise, I'm going to keep my comment there to clarify that being concerned about performance here requires a justification and we don't have one.
|
0

No. If you want essy to write code, copy to a second array but miss off the last char. You can use a char pointer in the first case to perhaps save some memory

Comments

0

The terminating nul will be omitted if it doesn't fit. Since your strings are all fixed length, that's not a problem to arrange. For example:

#include <stdio.h>

char foo[3][4] = { "four", ".by." , "3333" };

int main(void)
{
    for (int i = 0; i < 3; ++i)
    {
         for (int j = 0; j < 4; ++j)
             putchar(foo[i][j]);
         putchar('\n');
    }
}

3 Comments

If I try to compile this I get error: initializer-string for array of chars is too long
Weird. Compiling as C++, I get this too.
0

There is no way to have a string literal not null terminated.

But you actually want to pack a number of strings densely and you know both the sizes and the strings at development time.

Assume:

"First"

"Second"

"Third"

to pack them you can safely do:

char const s[] = "First""Second""Third";

You only need to save lengths and properly reconstruct termination in case you want to print or use std string. That is easy though.

As a bonus you have saved from the excess pointer you would have to store for each and everyone string needed.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.