bug-gnu-utils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: libgettext.h (Re: Documentation of ngettext)


From: Alexandre Duret-Lutz
Subject: Re: libgettext.h (Re: Documentation of ngettext)
Date: 12 Dec 2001 16:13:17 +0100
User-agent: Gnus/5.0808 (Gnus v5.8.8) Emacs/21.1

>>> "Bruno" == Bruno Haible <address@hidden> writes:

 Bruno> Alexandre Duret-Lutz writes:
 >> Those `(char *)' casts will cause warnings everytime I use a
 >> litteral string.

 Bruno> Could you please tell for which piece of code you get which warning?
 Bruno> Do you get a warning for
 Bruno> char* s = gettext("foobar");
 Bruno> or for
 Bruno> char* s = ngettext("sing","plural",n)
 Bruno> ?

I get a warning for both lines, hopefully, because these two
statements are dubious: in both cases a `const char*' is
assigned (or casted in the second case) to `char *'.

What I was trying to say in the previous message is that because
the return type of gettext() and ngettext() ought to be `const
char*' (even if it's not actually the case), people ought to
treat it as so and write:

  const char *s = gettext("foobar");
or
  const char *s = ngettext("sing","plural",n);

On these lines I get a warning for the `ngettext()' call, since
it performs the `char *' cast, but not on the `gettext()' call.


~/tmp % cat -n f.c
     1  # define gettext(Msgid) (Msgid)
     2  # define ngettext(Msgid1, Msgid2, N) \
     3      ((N) == 1 ? (char *) (Msgid1) : (char *) (Msgid2))
     4  
     5  void foo(void)
     6  {
     7          char* s1 = gettext("foobar");
     8          char* s2 = ngettext("sing","plural", 0);
     9          const char* c1 = gettext("foobar");
    10          const char* c2 = ngettext("sing","plural", 0);
    11  
    12  }
~/tmp % gcc -Wwrite-strings -Wcast-qual -c f.c
f.c: In function `foo':
f.c:7: warning: initialization discards qualifiers from pointer target type
f.c:8: warning: cast discards qualifiers from pointer target type
f.c:8: warning: cast discards qualifiers from pointer target type
f.c:10: warning: cast discards qualifiers from pointer target type
f.c:10: warning: cast discards qualifiers from pointer target type

I'm fine with the warnings on lines 7 and 8, these are usefull,
but not on line 10.

If, as suggested, you remove those casts, the annoying warning
disappear and the usefull warnings remain:

~/tmp % cat -n f.c
     1  # define gettext(Msgid) (Msgid)
     2  # define ngettext(Msgid1, Msgid2, N) ((N) == 1 ? (Msgid1) : (Msgid2))
     3  
     4  
     5  void foo(void)
     6  {
     7          char* s1 = gettext("foobar");
     8          char* s2 = ngettext("sing","plural", 0);
     9          const char* c1 = gettext("foobar");
    10          const char* c2 = ngettext("sing","plural", 0);
    11  
    12  }
~/tmp % gcc -Wwrite-strings -Wcast-qual -c f.c
f.c: In function `foo':
f.c:7: warning: initialization discards qualifiers from pointer target type
f.c:8: warning: initialization discards qualifiers from pointer target type

Amen :)
-- 
Alexandre Duret-Lutz



reply via email to

[Prev in Thread] Current Thread [Next in Thread]