pointer to function question

Discussion in 'C Programming' started by mdh, May 15, 2007.

  1. mdh

    mdh Guest

    I am curious as to why the lines commented out also seem to work?( I
    thought that the declaration of the pointer needed to mimic the
    function it is supposed to point to, hence I expected void(*p)(char)
    to work, which it does.)

    #include <stdio.h>

    int main (int argc, const char * argv[]) {
    void myf(char);
    void (*p)(char);
    /* also works: void (*p)(); */
    /* also works: int (*p)(); */

    p=myf;
    p('u');
    return 0;
    }


    void myf( char c){
    printf("Character is %c\n", c);
    }
    mdh, May 15, 2007
    #1
    1. Advertising

  2. mdh

    Ian Collins Guest

    mdh wrote:
    > I am curious as to why the lines commented out also seem to work?( I
    > thought that the declaration of the pointer needed to mimic the
    > function it is supposed to point to, hence I expected void(*p)(char)
    > to work, which it does.)
    >

    Pot luck, the correct value happens to end up the the right place. Did
    you note the warnings your compiler gave you?

    > #include <stdio.h>
    >
    > int main (int argc, const char * argv[]) {
    > void myf(char);
    > void (*p)(char);
    > /* also works: void (*p)(); */
    > /* also works: int (*p)(); */
    >
    > p=myf;
    > p('u');
    > return 0;
    > }
    >
    >
    > void myf( char c){
    > printf("Character is %c\n", c);
    > }
    >



    --
    Ian Collins.
    Ian Collins, May 15, 2007
    #2
    1. Advertising

  3. mdh

    mdh Guest


    > mdh wrote:
    > > I am curious as to why the lines commented out also seem to work?..........


    > > #include <stdio.h>

    >
    > > int main (int argc, const char * argv[]) {
    > > void myf(char);
    > > void (*p)(char);
    > > /* also works: void (*p)(); */
    > > /* also works: int (*p)(); */

    >
    > > p=myf;
    > > p('u');
    > > return 0;
    > > }

    >
    > > void myf( char c){
    > > printf("Character is %c\n", c);
    > > }




    Ian Collins <> wrote:
    > Pot luck, the correct value happens to end up the the right place. Did
    > you note the warnings your compiler gave you?



    warning..."assignment from incompatible pointer type".

    So, the fact that it gives the "correct" result is just plain luck?
    mdh, May 15, 2007
    #3
  4. mdh <> writes:
    >> mdh wrote:
    >> > I am curious as to why the lines commented out also seem to
    >> > work?..........

    >
    >> > #include <stdio.h>

    >>
    >> > int main (int argc, const char * argv[]) {
    >> > void myf(char);
    >> > void (*p)(char);
    >> > /* also works: void (*p)(); */
    >> > /* also works: int (*p)(); */

    >>
    >> > p=myf;
    >> > p('u');
    >> > return 0;
    >> > }

    >>
    >> > void myf( char c){
    >> > printf("Character is %c\n", c);
    >> > }

    >
    >
    >
    > Ian Collins <> wrote:
    >> Pot luck, the correct value happens to end up the the right place. Did
    >> you note the warnings your compiler gave you?

    >
    >
    > warning..."assignment from incompatible pointer type".
    >
    > So, the fact that it gives the "correct" result is just plain luck?


    Yes, *bad* luck. (If you had *good* luck, your implementation would
    catch the error earlier.)

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, May 15, 2007
    #4
  5. mdh said:

    > I am curious as to why the lines commented out also seem to work?


    Experimentation is often a very useful tool in programming, but in
    learning a programming language it is a definite handicap.

    If you're writing a novel in English, you might well try out something
    like "Pete, a pipe, a Pict, a peck of pickled pepper" just to see
    whether it pans out as you wanted it to, and that's legitimate.

    But if you are *learning* English, it isn't good enough to string words
    together almost at random and wonder whether they work. They mite eve
    an sow and rite too ewe, but communicating with another English-speaker
    in that way is fraught with peril. So it is with the compiler.
    Especially if you silence its attempts to warn you.

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at the above domain, - www.
    Richard Heathfield, May 15, 2007
    #5
  6. mdh

    mdh Guest

    On May 14, 10:24 pm, Richard Heathfield <> wrote:
    > mdh said:
    >
    > > I am curious as to why the lines commented out also seem to work?

    >
    > Experimentation is often a very useful tool in programming, but in
    > learning a programming language it is a definite handicap.
    >
    >



    ok....point taken. Thanks.
    mdh, May 15, 2007
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. glen stark
    Replies:
    2
    Views:
    703
    Ron Natalie
    Oct 10, 2003
  2. Fraser Ross
    Replies:
    4
    Views:
    1,043
    Fraser Ross
    Aug 14, 2004
  3. Replies:
    10
    Views:
    697
    Chris Torek
    Feb 4, 2005
  4. murgan
    Replies:
    6
    Views:
    4,861
    Thad Smith
    Dec 21, 2005
  5. Vijai Kalyan
    Replies:
    4
    Views:
    704
    Vijai Kalyan
    Nov 8, 2005
Loading...

Share This Page