Menu

#2496 function pointers to functions with struct pointer arguments do not work.

closed-fixed
Ben Shi
Front-end
5
2016-04-25
2016-04-23
Kio
No

function pointers to functions with struct pointer arguments do not work.

compiling uploaded file foo.c with
sdcc foo.c
results in error:

foo.c:13: error 78: incompatible types
from type 'unsigned-int function ( struct Data generic* fixed) fixed'
  to type 'unsigned-int function ( struct Data generic* fixed) fixed'

tested in sdcc version 3.4 and in current nightly build.

1 Attachments

Discussion

  • Ben Shi

    Ben Shi - 2016-04-23
    • status: open --> closed-rejected
     
  • Ben Shi

    Ben Shi - 2016-04-23

    Your foo.c is wrong. Even gcc complaints with

    $ gcc foo.c -c
    foo.c:8:34: warning: ‘struct Data’ declared inside parameter list
     typedef unsigned int (*T)(struct Data*);
                                      ^
    foo.c:8:34: warning: its scope is only this definition or declaration, which is probably not what you want
    foo.c:13:9: warning: initialization from incompatible pointer type
     T bar = foo;
             ^
    

    If you change it to

    struct Data;
    typedef unsigned int (*T)(struct Data*);
    
    struct Data { unsigned int a; };
    
    extern unsigned int foo(struct Data* f);
    T bar = foo;
    

    Both gcc and sdcc (the newest snapshot build) accept it.

    The key reason is struct Data is neither defined nor declared before its reference in the definition of type T.

     
  • Ben Shi

    Ben Shi - 2016-04-23
    • Description has changed:

    Diff:

    --- old
    +++ new
    @@ -4,9 +4,11 @@
     sdcc foo.c 
     results in error:
    
    +~~~~
     foo.c:13: error 78: incompatible types
     from type 'unsigned-int function ( struct Data generic* fixed) fixed'
    -  to type 'unsigned-int function ( struct Data generic* fixed) fixed'
    +    to type 'unsigned-int function ( struct Data generic* fixed) fixed'
    +~~~~
    
     tested in sdcc version 3.4 and in current nightly build.
    
     
  • Ben Shi

    Ben Shi - 2016-04-23
    • Description has changed:

    Diff:

    --- old
    +++ new
    @@ -7,7 +7,7 @@
     ~~~~
     foo.c:13: error 78: incompatible types
     from type 'unsigned-int function ( struct Data generic* fixed) fixed'
    -    to type 'unsigned-int function ( struct Data generic* fixed) fixed'
    +  to type 'unsigned-int function ( struct Data generic* fixed) fixed'
     ~~~~
    
     tested in sdcc version 3.4 and in current nightly build.
    
     
  • Ben Shi

    Ben Shi - 2016-04-24

    Or you can delete the definition of struct Data, like

    //struct Data;
    typedef unsigned int (*T)(struct Data*);
    
    //struct Data { unsigned int a; };
    
    extern unsigned int foo(struct Data* f);
    T bar = foo;
    

    sdcc will treat the typedef unsigned int (*T)(struct Data*); as both definition of T and declaration of struct Data.

     
    • Kio

      Kio - 2016-04-24

      thanks for your explanations. the idea to test it with gcc didn't come to me, but the error message of gcc would have helped.
      I understand the problem, but let's say, a standard which provides syntax to define functions which are impossible to call is a little bit silly then...
      As i now understand why it did not work, i don't understand your last message and why sdcc accepts it. gcc rejects it and to my current understanding the compiler should reject it.

      typedef unsigned int (*T)(struct Data*);
      

      and

      extern unsigned int foo(struct Data* f);
      

      both define local data types which by that are different. (as gcc assures me.)
      finally sdcc also rejects my code if i actually try to call the function pointer with a struct Data later:

      typedef unsigned int (*T)(struct Data*); // local data type
      extern unsigned int foo(struct Data* f); // local data type
      T bar = foo;  // works in sdcc but shouldn't
      struct Data { unsigned int a; };
      int main() { struct Data d = {0}; foo(&d); } // rejected
      

      never mind,

      ... Kio !
      
       
  • Ben Shi

    Ben Shi - 2016-04-24
    • status: closed-rejected --> open
    • assigned_to: Ben Shi
     
  • Ben Shi

    Ben Shi - 2016-04-24

    sdcc should not accept

    T bar = foo;  // works in sdcc but shouldn't
    

    I will check it.

     
  • Ben Shi

    Ben Shi - 2016-04-25
    • status: open --> closed-fixed
     
  • Ben Shi

    Ben Shi - 2016-04-25

    Now sdcc revision #9578 rejects

    T bar = foo;  // works in sdcc but shouldn't
    
     

Log in to post a comment.