Re: Help needed in solving C-errors in Linux (gcc)

From: Josef Möllers (josef.moellers_at_fujitsu-siemens.com)
Date: 07/25/03


Date: Fri, 25 Jul 2003 08:51:54 +0200

Dominic Grosleau wrote:
>
> When i tried to compile this test driver for an actual conversion library
> i'm trying to make I got the following messages from gcc.
>
> /*problems reported*/
>
> gcc convertlib.c main.c -ansi -pedantic
>
> main.c:21:22: warning: character constant too long
> main.c:22:41: warning: multi-character character constant
> main.c: In function `convert':
> main.c:22: warning: comparison is always false due to limited range of data type
> main.c:22:61: warning: multi-character character constant
> main.c:22: warning: comparison is always false due to limited range of data type

> case 'imperial':

First of all, this is what the error message says it is: a
"multi-character character constant". A string (that's what you want,
but not what you can use here, see below) is delimited by double quotes:
"imperial".

However, you can't use a string in a switch statement. The switch
statement only works on "constant integer values".
You'll need to use nested if/else if's with strcmp:

if (strcmp(sys, "imperial") == 0)
{
    if (strcmp(initial, "inch") == 0 && strcmp(final, "feet") == 0)
        success = conv_inch2feet(unit);
}
else
    success = 1;

Thus said, I'd like to give some advice (this smells like homework ...):
use table-lookup:

struct {
    char *sys, *initial, *final;
    int (*convfunc)(int);
} convtable[] = {
    { "imperial", "inch", "feet", &conv_inch2feet },
    { NULL, NULL, NULL, NULL }
};

-- 
Josef Möllers (Pinguinpfleger bei FSC)
	If failure had no penalty success would not be a prize
						-- T.  Pratchett


Relevant Pages

  • Re: Lots of design issues needed.
    ... One possibility would be to replace the switch statement with an array ... > int showMenu() ... > int binarySearch(VPerson a, const string& x, const compare comp) const ...
    (alt.comp.lang.learn.c-cpp)
  • Re: member references
    ... then the next string would be the member name and sub member names etc. ... then use reflection to set/get the value. ... If you have a situation in which you think a switch statement does make sense, then rather than a plain index, you should probably use an enumeration. ... Why not just post a code sample that illustrates the basic idea. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: switch statement with the string being tested
    ... I have about 50 string values that can be in a string ... The switch statement seems to be a better choice then the ... int func; ... Visit http://www.ecomstation.de the home of german eComStation ...
    (comp.lang.c)
  • Re: how to improve perf. ?
    ... Put a ^ at the beginning to specify match at the beginning of the string. ... > work differently depending of the 5 first chars of it. ... > cause those first chars order doesn't matter, it can't be found in the ...
    (comp.lang.php)