How do I get rid of this warning?

Tony van der Hoff tony at vanderhoff.org
Mon Jan 8 14:47:53 GMT 2007


On 7 Jan at 21:52 Jan-Jaap van der Geer <janjaap at dsv.nl> wrote in message
<5d7ca7a14e.Jan-Jaap at iyonix.c2i.net>

> Hello,
> 
> When using GCC to compile this program:
> 
> #include "oslib/osword.h" #include "oslib/territory.h"
> 
> int main(void) {
>   oswordreadclock_utc_block utc;
>   territory_ordinals        ordinals;
> 
>   utc.op = oswordreadclock_OP_UTC;
>   xoswordreadclock_utc (&utc);
> 
>   xterritory_convert_time_to_ordinals (territory_CURRENT,
>                                        &utc.utc,
>                                        &ordinals);
> }
> 
> I get the warning:
> 
> warning: passing arg 2 of `xterritory_convert_time_to_ordinals' from
> incompatible pointer type
> 
> I suppose this is why: http://c-faq.com/ansi/constmismatch.html
> 
I think that is something different.

Repeating my post to c.s.a.p. for those who don't subscribe:

Well, taking one step at a time:

1. The construct used by OSLib is a formal promise from the library to the
caller that it won't mess about with the structure that it is being passed.
It is a perfectly valid use of the language.

2. If that promise were not made, then it would not be possible [1] to pass
a pointer to a genuinely const object. For that reason any suggestions to
remove the const qualifier in the prototype are ill-concieved.

3. The compiler is probably correct - if a bit over-zealous - in issuing a
warning. The pointer types are, after all, different. However, I would have
thought that the smart thing to do would be to silently allow it, as
Norcroft does.

4. It is odd that the compiler does not fault calls to, say, 
strcmp( const char *cs, const char *ct ). I surmise that it handles
typedef'd structures differently to native types.

5. Under the circumstances, I would recommend using the cast as the best
option. You may be able to get better support from comp.lang.c


[1] With or without a cast - It should be regarded as an error to cast away 
a const qualfier (i.e. widening the cast) [2], whereas casting in (i.e.
narrowing the cast) is perfectly acceptable.

[2] however, an experiment, reversing the const-ness, thus:

#include "oslib/osword.h"
#include "oslib/territory.h"

extern os_error *myxterritory_convert_time_to_ordinals (territory_t
territory,
      os_date_and_time *date_and_time,
      territory_ordinals *ordinals);

int main(void)
{
  const oswordreadclock_utc_block utc;
  territory_ordinals        ordinals;

  //utc.op = oswordreadclock_OP_UTC;
  //xoswordreadclock_utc (&utc);

  myxterritory_convert_time_to_ordinals (territory_CURRENT,
                                       &utc.utc,
                                       &ordinals);
  return 0;
}

produces the same warning, not an error. IMO, this is a deficiency. No doubt
others will disagree.


-- 
Tony van der Hoff        | mailto:tony at vanderhoff.org
Buckinghamshire, England 



More information about the oslib-user mailing list