Re: compilation warning with const parameter



In article <1171855586.368593.204590@xxxxxxxxxxxxxxxxxxxxxxxxxxxx>,
subramanian <subramanian100in@xxxxxxxxx> wrote:
Consider the following program:

#include <stdio.h>

void myfn(const int **a)
{

}

int main(void)
{
int *a[3];

myfn(a);

return 0;
}


the following Compilation warning is produced with gcc
const_ptr.c: In function `main':
const_ptr.c:27: warning: passing arg 1 of `myfn' from incompatible
pointer type

If I remove the const qualifier in myfn( ), the program compiles with
gcc without any warning.

Why is the warning generated with gcc ?

An array name mentioned outside of a & or sizeof construct decays
into a pointer to the first of its elements. It does -not- decay
into a const pointer to that element, and it does -not- const
qualify the elements of the array... not unless those elements
were declared const to begin with. The warning is thus correct:
you are passing something that is not declared const into a routine
that is expecting a const.


However with VC++ 2005, there is no compilation warning with the
original program ie even when the const qualifier is present.

Compilers are required to produce diagnostics if constraints
are violated, but whether those diagnostics are labeled as
"error" or "warning" or "note" or "Look At This!" is up to
the compiler.

A compiler is not required to stop compiling upon detecting something
interesting -- not unless it is an #error construct that is in full effect.

A compiler is allowed to produce any number of warnings or notes
that it wants, as long as it compiles code that does not violate
constaints.

The exact set of non-required diagnostics that a compiler produces
is a Quality of Implementation Issue (usually called QoI around here).
For the most part, compilers that produce valid diagnostics are
usually considered to be of higher quality than those which do not
bother to produce the diagnostic. We'll leave you to your own
conclusions as to what this implies about the relative worths
of gcc and VC++ 2005.
--
Is there any thing whereof it may be said, See, this is new? It hath
been already of old time, which was before us. -- Ecclesiastes
.



Relevant Pages

  • Re: a few doubts!
    ... the warning messages are diagnostics. ... >> So what C compiler did you use that produced this incorrect messages? ... That causes string literals to be treated as "const char*" rather than ...
    (comp.lang.c)
  • Re: something to do with void *
    ... Either a warning ... it's allowed to produce any additional diagnostics it likes. ... If a compiler in conforming mode doesn't produce a diagnostic for the ... to have a void expression in some contexts, ...
    (comp.lang.c)
  • Re: List<> of struct with property. Cannot change value of property. why?
    ... temporary), the compiler could complain and say: ... Well, IMHO the compiler could easily provide the same warning today, without the "const" keyword. ... Relying on the "const" keyword to enable a warning wouldn't have been a good idea in C++, because you'd get a lot of false positives due to the large amount of code that is "const" without using "const". ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: default int
    ... it would assume that you meant to make the function return int. ... As you should know, constraint violations must be detected by the compiler, and at least one "diagnostic message" must be output. ... Both warnings and errors are types of diagnostics. ... If you get a warning from your compiler, ...
    (comp.lang.c)
  • Re: Strange - a simple assignment statement shows error in VC++ but works in gcc !
    ... Well, the declaration of p, if a compiler accepts it, invokes ... indicating that it's been declared as "const". ... could issue optional diagnostics for code that modifies an object of ...
    (comp.lang.c)