Hacker News new | ask | show | jobs
by acqq 716 days ago
> the C example ... by definition strings cannot contain that character

They can, only some functions stop at the first zero:

    char s[] = "gogo\x00gogo";
    printf( "%d\n", (int)sizeof( s ) );
2 comments

sizeof s (fun fact: you can omit the parentheses because it's not a function, it's an operator) reports the size of the array s, which is a variable of automatic storage duration.

Consider the following program:

  #include <stdio.h>
  #include <string.h>

  int main()
  {
      const char * const s = "gogo\x00gogo";
      const char t[] = "gogo\x00gogo";
      printf("%d,%d,%d,%d\n", 
          (int)sizeof s, 
          (int)strlen(s), // DO NOT EVER USE THIS FUNCTION
                          // YOU WILL BE FIRED IF YOU DO
          (int)sizeof t,
          (int)strlen(t)  // DO NOT EVER USE THIS FUNCTION
                          // YOU WILL BE FIRED IF YOU DO
      );
  }

  > 8,4,10,4
> > the C example ... by definition strings cannot contain that character

> They can, only some functions stop at the first zero:

> char s[] = "gogo\x00gogo";

And in what human language is that `\x00` a valid character? What rune/glyph/symbol represents it?

Nothing, you say? Well, looks like a good choice to me.