function pointer and pointer to function

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • prashant.khade1623@gmail.com

    function pointer and pointer to function

    Hi all,

    Are function pointer and pointer to a function same ?

    How do we declare a function pointer ?

    How to declare an array of function pointers ?

    Can you please give some examples for these.

    Thanks,
  • Philip Potter

    #2
    Re: function pointer and pointer to function

    prashant.khade1 623@gmail.com wrote:
    Hi all,
    >
    Are function pointer and pointer to a function same ?
    Yes.
    How do we declare a function pointer ?
    It depends on the type of function you want to point to. For example, to
    declare fp as a pointer to a function taking char * and returning int,
    you'd say:

    int (*fp)(char *);

    All parenthesis () are necessary. If you say:

    int *fp(char *);

    then you're declaring fp as a function taking 'char *' and returning
    'int *'.
    How to declare an array of function pointers ?
    When things get more complicated, such as arrays of function pointers,
    or pointers to functions taking pointers to functions and returning a
    pointer to function, you can still declare them directly, but you can
    also do it in stages using typedefs. There's a wonderful example in the
    FAQ, question 1.21.

    Philip

    Comment

    • prashant.khade1623@gmail.com

      #3
      Re: function pointer and pointer to function

      On Apr 10, 4:17 pm, Philip Potter <p...@doc.ic.ac .ukwrote:
      prashant.khade1 ...@gmail.com wrote:
      Hi all,
      >
      Are function pointer and pointer to a function same ?
      >
      Yes.
      >
      How do we declare a function pointer ?
      >
      It depends on the type of function you want to point to. For example, to
      declare fp as a pointer to a function taking char * and returning int,
      you'd say:
      >
      int (*fp)(char *);
      >
      All parenthesis () are necessary. If you say:
      >
      int *fp(char *);
      >
      then you're declaring fp as a function taking 'char *' and returning
      'int *'.
      >
      How to declare an array of function pointers ?
      >
      When things get more complicated, such as arrays of function pointers,
      or pointers to functions taking pointers to functions and returning a
      pointer to function, you can still declare them directly, but you can
      also do it in stages using typedefs. There's a wonderful example in the
      FAQ, question 1.21.
      >
      Philip
      thanks for your help...

      I need to know about declaring array of function pointers and how to
      use them

      Comment

      • Philip Potter

        #4
        Re: function pointer and pointer to function

        prashant.khade1 623@gmail.com wrote:
        On Apr 10, 4:17 pm, Philip Potter <p...@doc.ic.ac .ukwrote:
        >prashant.khade 1...@gmail.com wrote:
        >>How to declare an array of function pointers ?
        >When things get more complicated, such as arrays of function pointers,
        >or pointers to functions taking pointers to functions and returning a
        >pointer to function, you can still declare them directly, but you can
        >also do it in stages using typedefs. There's a wonderful example in the
        >FAQ, question 1.21.
        >>
        >Philip
        >
        thanks for your help...
        >
        I need to know about declaring array of function pointers and how to
        use them
        I have already answered your question. I have quoted my answer above. If
        you still don't understand, ask more specific questions - if you ask the
        same question again, I can't tell which bit of my answer you haven't
        understood.

        Comment

        • Peter 'Shaggy' Haywood

          #5
          Re: function pointer and pointer to function

          Groovy hepcat Philip Potter was jivin' in comp.lang.c on Fri, 11 Apr
          2008 5:30 pm. It's a cool scene! Dig it.
          prashant.khade1 623@gmail.com wrote:
          >On Apr 10, 4:17 pm, Philip Potter <p...@doc.ic.ac .ukwrote:
          >>prashant.khad e1...@gmail.com wrote:
          >>>How to declare an array of function pointers ?
          >>When things get more complicated, such as arrays of function
          >>pointers, or pointers to functions taking pointers to functions and
          >>returning a pointer to function, you can still declare them
          >>directly, but you can also do it in stages using typedefs. There's a
          >>wonderful example in the FAQ, question 1.21.
          >>>
          >>Philip
          >>
          >thanks for your help...
          >>
          >I need to know about declaring array of function pointers and how to
          >use them
          >
          I have already answered your question. I have quoted my answer above.
          If you still don't understand, ask more specific questions - if you
          ask the same question again, I can't tell which bit of my answer you
          haven't understood.
          True the OP *could* declare an array of pointer to function by using
          typedef, but I think he wants to declare it directly. It's easy enough.
          You haven't forgotten how to do it, have you, Phil? :)

          int (*fpa[10])(int);

          This declares an array of 10 pointers to function taking and int and
          returning an int.

          --
          Dig the sig!

          ----------- Peter 'Shaggy' Haywood ------------
          Ain't I'm a dawg!!

          Comment

          Working...