Home > programming > Embedding NULL in a C string

Embedding NULL in a C string

November 18th, 2008

I just solved this interesting little problem for myself, and it took me a little while (not that long, but long enough) to get it right and straight in my head. The problem at hand is this: suppose you want to embed a NULL character (for whatever reason) into a C string. That’s a bit of a quandary, since C uses NULL (a.k.a. ASCII 0, or \0) for string termination. So if you try something like:

printf("This string is\0awesome!\n");

You’ll be left in the cold wondering what this string is, because you’ll never see anything after the “is.” That’s right, you’ll never get to know that this string is, in fact, awesome. That’s rather annoying if, for example, you want to use NULL as a delimiter for something. One might want to do this since NULL characters are disallowed in things like file names (since they terminate them). So the question is “can we fix it?” And much to my own glee, the answer is “yes we can!”

My first attempt was to encode a %s conversion specifier in there and make its value “\0″, like so:

printf("This string is%sawesome!\n", "\0");

It’ll print out the full string with that invisible NULL, right? Unfortunately, this is incorrect. It will print the full string, but not the NULL. Why? Because the %s will look at its corresponding string, cut off the NULL character, and insert it. So it puts in a string of length 0 in there. So it looks like nothing has been put into the string, because it has. But nothing != NULL. Back to the drawing board.

My next attempt was to encode a %c conversion specifier into the string and make its value ‘\0′, like so:

printf("This string is%cawesome!\n", '\0');

At first, it seemed to work, but I was dismayed. All I saw was the output of the original try (“This string is”). I guess that’s what I wanted, right? But it appears it didn’t do anything with the rest of the string. What to do? Well, I turned to printf’s close cousin, sprintf, to put the value into a string for later manipulation and printing. So I malloc’d some memory for a char *mystr, like so:

char *mystr = malloc(64); // arbitrary amount of memory

Then I shoved my string into mystr:

sprintf(mystr, "This string is%cawesome!\n", '\0');

Then I put in a line to print mystr:

printf("%s\n", mystr);

And… failure? Damn. I must be doing something wrong… Then it hit me: printf() will always stop at the NULL character, but that doesn’t mean my string isn’t all there. The key here is that I allocated enough memory for that string, so I’ll be damned if my whole string isn’t there. To test my theory, I modified my line like so:

printf("%s", mystr+15);

For those of you who aren’t all that up on your C, since mystr is simply a pointer to a location in memory to the beginning of my characters, mystr+15 is that location plus 15 bytes, which is one character after the inserted NULL byte. As I expected (and hoped), “awesome!” (with a newline) was printed! Success! Now, you might think back to my first attempt and wonder if that would work with sprintf, but it does not. It’s probably for the best anyway, since %c makes more of a point that you’re doing something despicable like puttying NULL characters into your strings, and allows for easier changing if you decide to use something else in the future.

So a final question comes up: if I’m shoving the character that’s used for string termination in my strings, how do I know when it’s really done? Well that’s a matter for a particular implementation. In mine, the newline character will be my buddy. For another implementation, one might decide to use calloc() instead and just look for more than one NULL character in a row. Either way it’s rather messy, but for my purposes I think it’ll work just fine.

Related Links:

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • StumbleUpon
  • Reddit
  • Technorati

programming ,

  1. No comments yet.
  1. No trackbacks yet.