HELP: function pointer

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • emanuela

    #1

    HELP: function pointer

    Hi,
    I've a little question about C language:

    according to the pthread_create signature:
    int pthread_create( pthread_t *thread,
    const pthread_attr_t *attr,
    void *(*start_routin e)(void*),
    void *arg);
    which syntax is more correct?

    1) void* function (void* args) {...}

    main()
    { ...
    pthread_create( &mythread, NULL, function, NULL)
    ...
    }

    2) void* function (void* args) {...}

    main()
    { ...
    pthread_create( &mythread, NULL, &function, NULL)
    ...
    }

    According to me, only the second one is correct
    but I succeed in compiling both of them (gcc - Linux Fedora Core 1 (2.4.))
    Why?

    Thanks

    Emanuela
  • Simon Massey

    #2
    Re: function pointer

    "emanuela" <em614@virgilio .it> wrote in message
    news:833383f9.0 406140254.7865e 63d@posting.goo gle.com...[color=blue]
    > Hi,
    > I've a little question about C language:
    > which syntax is more correct?
    >
    > 1) void* function (void* args) {...}
    >
    > main()
    > { ...
    > pthread_create( &mythread, NULL, function, NULL)
    > pthread_create( &mythread, NULL, &function, NULL)
    > ...
    > }
    >
    > According to me, only the second one is correct
    > but I succeed in compiling both of them (gcc - Linux Fedora Core 1 (2.4.))
    > Why?[/color]

    Both are correct (perhaps &function, slightly more so) as the name of a
    function is it's address. The leading & is ignored with function names as
    the only thing you can do with a function is call it (using ()) or take it's
    address.


    Comment

    • red floyd

      #3
      Re: HELP: function pointer

      em614@virgilio. it (emanuela) wrote in message news:<833383f9. 0406140254.7865 e63d@posting.go ogle.com>...[color=blue]
      > Hi,
      > I've a little question about C language:
      >
      > according to the pthread_create signature:
      > int pthread_create( pthread_t *thread,
      > const pthread_attr_t *attr,
      > void *(*start_routin e)(void*),
      > void *arg);
      > which syntax is more correct?
      >
      > 1) void* function (void* args) {...}
      >
      > main()
      > { ...
      > pthread_create( &mythread, NULL, function, NULL)
      > ...
      > }
      >
      > 2) void* function (void* args) {...}
      >
      > main()
      > { ...
      > pthread_create( &mythread, NULL, &function, NULL)
      > ...
      > }[/color]

      Either one is correct. IIRC, in the original K&R, you got a warning
      with the second form. The naked name of a function (without parens)
      refers to its address.

      Comment

      • Mark A. Odell

        #4
        Re: HELP: function pointer

        em614@virgilio. it (emanuela) wrote in
        news:833383f9.0 406140254.7865e 63d@posting.goo gle.com:
        [color=blue]
        > Hi,
        > I've a little question about C language:
        >
        > according to the pthread_create signature:
        > int pthread_create( pthread_t *thread,
        > const pthread_attr_t *attr,
        > void *(*start_routin e)(void*),
        > void *arg);
        > which syntax is more correct?
        >
        > 1) void* function (void* args) {...}
        >
        > main()
        > { ...
        > pthread_create( &mythread, NULL, function, NULL)
        > ...
        > }
        >
        > 2) void* function (void* args) {...}
        >
        > main()
        > { ...
        > pthread_create( &mythread, NULL, &function, NULL)
        > ...
        > }
        >
        > According to me, only the second one is correct
        > but I succeed in compiling both of them (gcc - Linux Fedora Core 1
        > (2.4.)) Why?[/color]

        Because you are wrong. Similar to how you can use char array[10]; 'array'
        as the address of the first element or '&array[0]' so too can you use
        'function' or '&function'. Also, your main() is wrong, it must return
        'int'. Do not rely on implicit 'returns int' of non-prototyped functions.

        --
        - Mark ->
        --

        Comment

        • Jack Klein

          #5
          Re: function pointer

          On Mon, 14 Jun 2004 12:52:23 +0100, "Simon Massey"
          <sma@prismtechn ologies.com> wrote in comp.lang.c:
          [color=blue]
          > "emanuela" <em614@virgilio .it> wrote in message
          > news:833383f9.0 406140254.7865e 63d@posting.goo gle.com...[color=green]
          > > Hi,
          > > I've a little question about C language:
          > > which syntax is more correct?
          > >
          > > 1) void* function (void* args) {...}
          > >
          > > main()
          > > { ...
          > > pthread_create( &mythread, NULL, function, NULL)
          > > pthread_create( &mythread, NULL, &function, NULL)
          > > ...
          > > }
          > >
          > > According to me, only the second one is correct
          > > but I succeed in compiling both of them (gcc - Linux Fedora Core 1 (2.4.))
          > > Why?[/color]
          >
          > Both are correct (perhaps &function, slightly more so) as the name of a
          > function is it's address. The leading & is ignored with function names as
          > the only thing you can do with a function is call it (using ()) or take it's
          > address.[/color]

          No, the name of a function is not an address, anymore than the name of
          an array is an address. It is a "function designator", defined as
          such by paragraph 4 of 6.3.2.1 of the current C standard:

          "A function designator is an expression that has function type. Except
          when it is the operand of the sizeof operator(54) or the unary &
          operator, a function designator with type ‘‘function returning type’’
          is converted to an expression that has type ‘‘pointer to
          function returning type’’."

          So a function designator is automatically and silently converted to a
          function pointer in all contexts except when it is used as the operand
          of the sizeof or & operators. The use of the & operator, as when this
          operator is used on an lvalue of array type, explicitly converts the
          name to a pointer.

          The sizeof operator applied to a function designator, unlike applying
          it to an lvalue of array type, is invalid, as stated explicitly in the
          text of footnote 54, referenced above:

          "54) Because this conversion does not occur, the operand of the sizeof
          operator remains a function designator and violates the constraint in
          6.5.3.4."

          So given:

          #include <stdio.h>
          int func(int x)
          {
          return x + 1;
          }

          int (*fptr)(int) = func;

          int main(void)
          {
          printf("size of function pointer %lu\n",
          (unsigned long)sizeof(fpt r));
          printf("size of function %lu\n",
          (unsigned long)sizeof(fun c));
          return 0;
          }

          ....a conforming compiler must issue a diagnostic for the sizeof
          expression in the second printf() call.

          As shown by:

          Error c:\prog\lcc\pro jects\sample\sa mple.c: 14 invalid type argument
          'int function(int)' to 'sizeof'

          ....from one compiler and this:

          Compiling...
          sample.c
          c:\program files\microsoft visual
          studio\myprojec ts\sample\sampl e.c(14) : error C2070: illegal sizeof
          operand
          Error executing cl.exe.

          sample.obj - 1 error(s), 0 warning(s)

          ....from another.

          --
          Jack Klein
          Home: http://JK-Technology.Com
          FAQs for
          comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
          comp.lang.c++ http://www.parashift.com/c++-faq-lite/
          alt.comp.lang.l earn.c-c++

          Comment

          • emanuela

            #6
            Re: HELP: function pointer

            thanks

            Emanuela

            "Mark A. Odell" <odellmark@hotm ail.com> wrote in message news:<Xns95086F 34AE6E5Copyrigh tMarkOdell@130. 133.1.4>...[color=blue]
            > em614@virgilio. it (emanuela) wrote in
            > news:833383f9.0 406140254.7865e 63d@posting.goo gle.com:
            >[color=green]
            > > Hi,
            > > I've a little question about C language:
            > >
            > > according to the pthread_create signature:
            > > int pthread_create( pthread_t *thread,
            > > const pthread_attr_t *attr,
            > > void *(*start_routin e)(void*),
            > > void *arg);
            > > which syntax is more correct?
            > >
            > > 1) void* function (void* args) {...}
            > >
            > > main()
            > > { ...
            > > pthread_create( &mythread, NULL, function, NULL)
            > > ...
            > > }
            > >
            > > 2) void* function (void* args) {...}
            > >
            > > main()
            > > { ...
            > > pthread_create( &mythread, NULL, &function, NULL)
            > > ...
            > > }
            > >
            > > According to me, only the second one is correct
            > > but I succeed in compiling both of them (gcc - Linux Fedora Core 1
            > > (2.4.)) Why?[/color]
            >
            > Because you are wrong. Similar to how you can use char array[10]; 'array'
            > as the address of the first element or '&array[0]' so too can you use
            > 'function' or '&function'. Also, your main() is wrong, it must return
            > 'int'. Do not rely on implicit 'returns int' of non-prototyped functions.[/color]

            Comment

            Working...