Hello,

The following code is rejected by one compiler, while it is accepted by gcc
without any warning. Several people in comp.lang.c seem to think that it is a
bug in the first compiler which should ***not*** reject the program.

Message-ID: <h63e5s$ec...@aioe.org>
http://groups.google.com/group/comp.lang.c/browse_frm/thread/2858a1c9ccdcd741

I'd like to ask what you think.

typedef int fun_t(int *p);

int foo1(      int *      p) { return *p; }
int foo2(const int *      p) { return *p; }
int foo3(      int *const p) { return *p; }
int foo4(const int *const p) { return *p; }

void zozo(void)
{
  fun_t *fp;
  fp = foo1;
  fp = foo2; /* GCC WARNS */
  fp = foo3;
  fp = foo4; /* GCC WARNS */
}

(I run gcc 4.3.2 under cygwin.)

$ gcc -std=c89 -pedantic -Wall -Wextra -O2 -c mu2.c
mu2.c: In function 'zozo':
mu2.c:12: warning: assignment from incompatible pointer type
mu2.c:14: warning: assignment from incompatible pointer type

$ cc -c mu2.c
E "mu2.c",L12/C8(#416): foo2
|    Type `int(const int * p)' ("mu2.c",L4/C5) can't be converted to type
`fun_t(*)'.
|    (See also type `fun_t' (= `int(int * p)') ("mu2.c",L1/C13)).
E "mu2.c",L13/C8(#416): foo3
|    Type `int(int *const p)' ("mu2.c",L5/C5) can't be converted to type 
`fun_t(*)'.
|    (See also type `fun_t' (= `int(int * p)') ("mu2.c",L1/C13)).
E "mu2.c",L14/C8(#416): foo4
|    Type `int(const int *const p)' ("mu2.c",L6/C5) can't be converted to type
`fun_t(*)'.
|    (See also type `fun_t' (= `int(int * p)') ("mu2.c",L1/C13)).
3 user errors   No warnings

The relevant line is line 13 i.e. fp = foo3;
(cc's warnings are cosmetically different if I write fp = &foo3;)

E "mu2.c",L13/C8(#416):
|    Type `int(*)(int *const p)' can't be converted to type `fun_t(*)'.
|    (See also type `int(int *const p)' ("mu2.c",L5/C5)).
|    (See also type `fun_t' (= `int(int * p)') ("mu2.c",L1/C13)).

In short, cc refuses to convert
an "int (*)(int *const)" pointer to an "int (*)(int *)" pointer.

Would you say this is a bug in cc?

-- 
Regards.

Reply via email to