S
s0suk3
gcc is displaying a warning that confuses me. The warning goes away if
I cast the expression in the return statement in f() to a const-
qualified type (in this case, to const char**):
#include <stdio.h>
typedef struct A
{
char** argv;
} A;
const char** f(const A* a)
{
return (char**) a->argv + 1;
}
int main(int argc, char* argv[])
{
A a = {argv};
printf("%s\n", *f(&a));
return 0;
}
$ gcc warning.c
warning.c: In function 'f':
warning.c:10: warning: return from incompatible pointer type
But the warning suggests that the problem is in the return type rather
than in the cast (which seems strange, since a particular type ``T''
can always be converted to ``const T'').
What's even stranger is that there are no warnings for the following:
#include <stdio.h>
typedef struct A
{
char* argv0;
} A;
const char* f(const A* a)
{
return (char*) a->argv0 + 1;
}
int main(int argc, char* argv[])
{
A a = {argv[0]};
printf("%s\n", f(&a));
return 0;
}
Essentially the only difference is that this deals with char* rather
than char**, which doesn't explain why there's no warning anymore.
I think the confusion is compounded by the fact that there's a const-
qualified instance of a structure that has a non-const-qualified
member.
So what's the warning all about?
Sebastian
I cast the expression in the return statement in f() to a const-
qualified type (in this case, to const char**):
#include <stdio.h>
typedef struct A
{
char** argv;
} A;
const char** f(const A* a)
{
return (char**) a->argv + 1;
}
int main(int argc, char* argv[])
{
A a = {argv};
printf("%s\n", *f(&a));
return 0;
}
$ gcc warning.c
warning.c: In function 'f':
warning.c:10: warning: return from incompatible pointer type
But the warning suggests that the problem is in the return type rather
than in the cast (which seems strange, since a particular type ``T''
can always be converted to ``const T'').
What's even stranger is that there are no warnings for the following:
#include <stdio.h>
typedef struct A
{
char* argv0;
} A;
const char* f(const A* a)
{
return (char*) a->argv0 + 1;
}
int main(int argc, char* argv[])
{
A a = {argv[0]};
printf("%s\n", f(&a));
return 0;
}
Essentially the only difference is that this deals with char* rather
than char**, which doesn't explain why there's no warning anymore.
I think the confusion is compounded by the fact that there's a const-
qualified instance of a structure that has a non-const-qualified
member.
So what's the warning all about?
Sebastian