Bug 35928

Summary: type qualifiers ignored on function return type
Product: gcc Reporter: littlestar <cnstar9988>
Component: cAssignee: Not yet assigned to anyone <unassigned>
Status: RESOLVED INVALID    
Severity: normal CC: gcc-bugs
Priority: P3    
Version: 4.2.4   
Target Milestone: ---   
Host: Target:
Build: Known to work: 3.4.6
Known to fail: 4.2.2 4.2.3 4.2.4 Last reconfirmed:

Description littlestar 2008-04-14 02:35:55 UTC
the following code generate a warning on gcc 4.2.4

warning: type qualifiers ignored on function return type

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

typedef struct testabc{int class;}* THANDLE;

const THANDLE test()
{
        THANDLE x;

        x = (THANDLE)2;

        return x;
}

int main(int argc, char **argv)
{
     const THANDLE p = test();
     return (int)p;
}
Comment 1 Andrew Pinski 2008-04-14 03:33:02 UTC
The const in "const THANDLE" applies to the pointer type and not the element the pointer is pointing to.  So the warning is correct.
Comment 2 littlestar 2008-04-14 03:49:13 UTC
but how to applies to the pointer type and the element
the pointer is pointing to.

I don't want someone modify my pointer and the pointing to?

but the following code has no warning, why?

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

typedef struct testabc {int Class;}THANDLE;

const THANDLE* test()
{
        THANDLE *x;

        x = (THANDLE*)2;

        return x;
}

int main(int argc, char **argv)
{
     const THANDLE *p=  test();
        return (int)p;
}

Comment 3 Andrew Pinski 2008-04-14 04:10:57 UTC
"const THANDLE" in the first case is equivalant to "THANDLE* const" when THANDLE a struct.

> but how to applies to the pointer type and the element
> the pointer is pointing to.

With the typedefs, you can't.