Menu

#2641 Incompatible pointer types, even though one is void *

open
None
other
4
2022-04-14
2017-08-17
No

In SDCC #9994, I see the following error messages, when initalizing a void * from a different pointer type (there awas an error before, but in #9994 it was made more verbose to print the types involved).

static  struct { unsigned char STK[128]; } s;

void *v = &s + 1;

For the stm8 and z80 backends I see:

test.c:3: error 129: pointer types incompatible 
from type 'struct __00000000 near* fixed'
  to type 'void generic* fixed'
test.c:3: error 129: pointer types incompatible 
from type 'struct __00000000 near* fixed'
  to type 'void generic* const fixed'

Which looks a bit weird, since there is no near in the stm8 and stm8 backends. All pointers should be generic.

For the hc08 backend I see:

test.c:3: error 129: pointer types incompatible 
from type 'struct __00000000 xdata* xdata'
  to type 'void generic* xdata'
test.c:3: error 129: pointer types incompatible 
from type 'struct __00000000 xdata* xdata'
  to type 'void generic* const xdata'

And what is that 00000000?

Philipp

Discussion

  • Philipp Klaus Krause

    Even weirder, even the following gives silimar errors:

    static  struct { unsigned char STK[128]; } s;
    
    void *v = (void *)(s.STK + 1);
    

    PHilipp

     
  • Rajmund Szymański

    In this case:

    struct _s { unsigned char STK[128]; };
    
    static struct _s s;
    
    void *v = (struct _s *)&s + 1;
    

    there are no errors.

     
  • Philipp Klaus Krause

    A closer look at the ISO C11 standard, section 6.6 indicates that SDCC does not have to accept '+' in address pointer constants. In that case the bug is not SDCC rejecting the code, but rather SDCC giving a wrong error message.

    Philipp

     
  • Philipp Klaus Krause

    • Priority: 5 --> 4
     
  • Philipp Klaus Krause

    • assigned_to: Philipp Klaus Krause
     
  • Philipp Klaus Krause

    • status: open --> closed-fixed
     
  • Philipp Klaus Krause

    In revision [r10036], I changed the default error message in initPointer() to complain about the expression not being constant instead of incompatible types.

    Philipp

     
  • alvin

    alvin - 2017-10-08

    Does that mean your example is still an error?

    static  struct { unsigned char STK[128]; } s;
    void *v = (void *)(s.STK + 1);
    

    I think section 6.6 explicitly mentions this case:

    7 More latitude is permitted for constant expressions in initializers. Such a constant
    expression shall be, or evaluate to, one of the following:
    ...
    — an address constant for a complete object type plus or minus an integer constant
    expression.

     
    • Philipp Klaus Krause

      I was only looking at verse 9 of 6.6, but yes, verse 7 allows more. I'm not sure about the cast though.

      But it seems SDCC should at least accept

      static  struct { unsigned char STK[128]; } s;
      void *v = (s.STK + 1);
      

      So this bug is open again.

      Philipp

       
  • Philipp Klaus Krause

    • status: closed-fixed --> open
     
  • Rajmund Szymański

    Interesting example:

    struct _s { char t[10]; };
    struct _s s1;
    void *v1 = &s1 + 1;                // ERROR
    void *v2 = (struct _s *)&s1 + 1;   // OK
    
    void main()
    {
        struct _s s2;
        void *v3 = &s2 + 1;              // OK
        void *v4 = (struct _s *)&s2 + 1; // OK
        void *v5 = &s1 + 1;              // OK!
        void *v6 = (struct _s *)&s1 + 1; // OK! 
    }
    
     

    Last edit: Rajmund Szymański 2017-10-08
    • Philipp Klaus Krause

      Not much of a surprise. The file-scope objects need constant expressions as initializers, the function-scope auto ones do not.
      Even when this bug is fixed, still a lot more will be allowed for the function-scope auto ones, than for the file-scope ones.

       

Log in to post a comment.