break inside of case- statement inside of loop

  • Thread starter Alexander Korsunsky
  • Start date
P

Peter Nilsson

Keith Thompson said:
Default User said:
Bill said:
char array[10] = "abcdefghij";

It's unfortunate that this doesn't generate a
compiler warning in the same way that
int array[2] = { 1, 2, 3}; does.

I think the latter is a constraint violation. That's not true
for the first, it's perfectly legal, although questionable and
of course a warning could be issued if the compiler felt like
it.

For those not aware of the rule:

char s[] = "abc";
/* char s2[2] = "abc"; */
char s3[3] = "abc";
char s4[4] = "abc";
char s5[5] = "abc";

s is a 4-byte array containing { 'a', 'b', 'c', '\0' }.
s2, I believe, is a constraint violation.

Yes. [6.7.8p2 cf p14]
s3 is a 3-byte array containing { 'a', 'b', 'c' }; it is
*not* a string.

s4, like s, is a 4-byte array containing { 'a', 'b', 'c', '\0' }.
s5 is a 5-byte array containing { 'a', 'b', 'c', '\0', '\0' }.

The only way (I think) for a string literal to specify a non-
string (i.e., an array with no '\0' terminator) is to use it
as the initializer for an array of exactly the right size.

Note there's only a null byte terminator for the last string
literal in a sequence of concatenated strings...

puts("Hello" "World"); /* output HelloWord */
I suspect we'd be better off without this rule, which often
leads to incorrect code where the programmer *meant* to create
a valid string.

You say often, but is that really the case? I've been more
frustrated by C++'s stricter rule than the C's lax one.
Perhaps a special syntax to indicate that a string
literal represents an array not terminated with a '\0' would
have been useful.

I've been very happy with C's existing syntax and semantics in
the case of fixed width initialisors. But in any case, I can't
see the committee giving any consideration to a change.
 
J

Jack Klein

char array[10] = "abcdefghij";

It's unfortunate that this doesn't generate a
compiler warning in the same way that
int array[2] = { 1, 2, 3}; does.

[snip]

Why should it, when the definition with its initialization is
perfectly legal C?
 
K

Keith Thompson

Peter Nilsson said:
You say often, but is that really the case? I've been more
frustrated by C++'s stricter rule than the C's lax one.

I have no idea. I've seen it as a source of errors in code posted
here, but that's probably not representative of C programmers in
general. Personally, I've never felt the need to use this feature;
YMMV.
I've been very happy with C's existing syntax and semantics in
the case of fixed width initialisors. But in any case, I can't
see the committee giving any consideration to a change.

Agreed.
 
M

matevzb

...
And, to continue in this manner, it should probably be noted that
e.g.
s1024[1024] = "";
should not be used for string initialization, as it will set all
1024 characters to '\0'. Not usually noticed, but it may turn out
to cause a major performance degradation.

The golden rule is not to (micro-)optimise until there's a proven
bottleneck.

The real question is whether you actually need to initialise or
not. I find it extremely rare that I actually need to initialise
a character buffer to an empty string.

Given the choice, most programmers will not initialise an object
if they don't have to. [A notable exception is Richard Heathfield,
though I believe he is more likely to use {0} than "".]
It could be argued whether this is an optimization, to me it's just
common sense. As it turned out in my case, the programmer didn't know
this at all, the strings were really large (16K) and put in a function
that was called in a loop. Personally I prefer
char s1024[1024];
s1024[0] = '\0';
when initialization is required.
 
B

Bill Pursell

On Feb 25, 11:16 am, Alexander Korsunsky <[email protected]>
wrote:
char array[10] = "abcdefghij";
It's unfortunate that this doesn't generate a
compiler warning in the same way that
int array[2] = { 1, 2, 3}; does.

[snip]

Why should it, when the definition with its initialization is
perfectly legal C?


I think it is unfortunate that it is legal C. Having "..." as
an initializer strongly implies a string, and allowing the
resulting value to be not a string is, IMO, a source of
potential confusion.

I tend not to use initiliazations, but it seems more
readable to write:

char not_a_string[3] = {'a', 'b', 'c'};
than
char not_a_string[3] = "abc".

(Note: I'm NOT advocating for a change in the language, just
expressing my opinion on a relatively unimportant point.)
 
R

Richard Heathfield

Alexander Korsunsky said:

Is it possible to break out of the for loop from inside of one case-
statement, or do I have to use a workaround?

I find this question rather strange. We might paraphrase it as "is it
legal to make my code harder to read, understand, and maintain in this
particular way, or will I have to find some other way to make my code
harder to read, understand, and maintain?"

Surely the best solution is to arrange your code in such a way that you
don't need to have the control flow leaping around all over the place.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top