Re: Comparing string input to enum data type



Yevgen Muntyan wrote:
Nelu wrote:
Yevgen Muntyan wrote:
CBFalconer wrote:
Yevgen Muntyan wrote:
Bill Pursell wrote:

... snip ...
If you've red the string into a, and you want to set c:

if( 0 == strncmp("red", a, sizeof "red") )
c = red;
else if ...

Funny thing it actually works since 'sizeof "red"' is
greater than length of "red". Still, a rather strange
way to compare strings.

sizeof "red" had better be 4.

What do you mean? It doesn't matter if it's 4 or 15, as long
as it's greater than 3 (and it is).

sizeof "red" must be 4 no matter where you run the program. It
should give you the length of the array holding the string.

I say it 'sizeof "red" *is* 4', so I don't understand 'sizeof
"red" had better be 4'.

It's an English idiom, meaning that if sizeof "red" isn't 4
something is seriously wrong.

I restored the blank separator lines your software mistakenly
removed.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>


.



Relevant Pages

  • Re: Casting the return value of malloc()...
    ... The sizeof operator should never be followed immediately by '(' ... my view is that it is a matter far too small to ... The question of parentheses is a different matter. ...
    (comp.lang.c)
  • Re: [-mm] Add an owner to the mm_struct (v2)
    ... Doesn't make sense to switch them (spinlock is unsigned int on x86, ... what's sizeof between and after?)? ... Hmm, doesn't matter, there is another pointer after it, ignore me. ...
    (Linux-Kernel)
  • Re: C Strings
    ... A7.1 seems clear on the matter, but this wouldn't be the first time I ... its the optimiser which has a bug. ... The sizeof() operator should work out the size of its operand. ... n onto the string literal should force it to be converted to a pointer ...
    (comp.lang.c)
  • Re: 64 bit or 32 bit
    ... >>You should write your app in a way that it doesn't matter ... >>on which platform it runs. ... > same for sizeof, ...
    (comp.os.linux.development.system)
  • Re: Comparing string input to enum data type
    ... Still, a rather strange ... way to compare strings. ... sizeof "red" must be 4 no matter where you run the program. ...
    (comp.lang.c)