Hacker News new | past | comments | ask | show | jobs | submit login

Err... we teach C neophytes that you should never write values to variables that are larger than what the variables can hold. Don't write an int to a short, don't write a short to a char, and don't initialize five bytes to an array storing four bytes. Am I missing something here? char foo[4] = "ABCD" is always incorrect, no ifs and buts. If you want "readable" bytes, use character literals. You should never discount the null terminator.





Yes, you're missing something.

Sometimes I'll need an array of 4 ints, so I'll define one:

    int a[4] = {1,2,3,4};
other times I'll want 4 bytes. So sure, I can write:

    char a[4] = {'A','B','C','D'};
However, (I hope) I'll get the exact same compiler warning as the more readable:

    char a[4] = "ABCD";
that does the exact same. So I'll need the __nonstring__ anyway. And then why not use the more readable syntax, since I'm telling the compiler and reader explicitly that I don't want a null terminator?

The core issue is C's habit of using the exact same language construct for different purposes, here char[] for both uint8_array and null_terminated_str.


> However, (I hope) I'll get the exact same compiler warning as the more readable:

The latter is a null terminated string, the former is not. Compiler warnings are principally a set of heuristics for bad code. Heuristically the first example is more likely to be intentional than the latter.


Your char a[4] is not more readable and sooner or later you'll get screwed by strlen(a) or some such. It's quite telling that the construct is not legal in c++.

Why would I run strlen() on the second one but not on the first one? Presumably I know that I defined an array of chars and not a cstring? Or if I forget, couldn't I forget in the first case too? Once I defined it, they're both just char[4]s.



Join us for AI Startup School this June 16-17 in San Francisco!

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: