What is?

Discussion in 'C Programming' started by Shraddha, Jun 24, 2007.

  1. Shraddha

    Shraddha Guest

    What is this exactly...

    int(*(*ptr (int))(void)

    First I thought that this is the pointer to function...But I recognize
    that the syntax iss quite different...
    If we say that the function is taking void parameters i.e. no
    parameters then what "(int i)" is doing there...


    Please can some one help me out????
    Shraddha, Jun 24, 2007
    #1
    1. Advertising

  2. Shraddha wrote:
    > What is this exactly...
    >
    > int(*(*ptr (int))(void)
    >
    > First I thought that this is the pointer to function...But I recognize
    > that the syntax iss quite different...
    > If we say that the function is taking void parameters i.e. no
    > parameters then what "(int i)" is doing there...
    >
    >
    > Please can some one help me out????


    ptr is a pointer to a function which accepts a single argument of type int.
    This function returns a pointer to a function which accepts no arguments
    and returns an int. It is more readable when written as

    typedef int (*ptr_to_intfunc)(void);
    ptr_to_intfunc (*ptr(int));
    Harald van =?UTF-8?B?RMSzaw==?=, Jun 24, 2007
    #2
    1. Advertising

  3. Shraddha

    Guest

    On Jun 24, 10:27 pm, Harald van D k <> wrote:
    > Shraddha wrote:
    > > What is this exactly...

    >
    > > int(*(*ptr (int))(void)

    >
    > > First I thought that this is the pointer to function...But I recognize
    > > that the syntax iss quite different...
    > > If we say that the function is taking void parameters i.e. no
    > > parameters then what "(int i)" is doing there...

    >
    > > Please can some one help me out????

    >
    > ptr is a pointer to a function which accepts a single argument of type int.
    > This function returns a pointer to a function which accepts no arguments
    > and returns an int. It is more readable when written as
    >
    > typedef int (*ptr_to_intfunc)(void);
    > ptr_to_intfunc (*ptr(int));


    No, first there is a typo. it should have a respective closing
    parenthesis.

    int(*(*ptr (int)))(void)

    ptr is a function that takes int as an argument and returns a pointer
    to a pointer to a function that takes no arguments are returns an
    integer.
    , Jun 24, 2007
    #3
  4. Shraddha

    regis Guest

    Shraddha wrote:
    > What is this exactly...
    >
    > int(*(*ptr (int))(void)
    >
    > First I thought that this is the pointer to function...But I recognize
    > that the syntax iss quite different...
    > If we say that the function is taking void parameters i.e. no
    > parameters then what "(int i)" is doing there...


    Your parentheses are not balanced:
    There are three cases to rebalance them:

    1) you mean: int(*(*xxx ) (int)) (void) ;
    2) you mean: int(*(*yyy (int)) ) (void) ;
    3) you mean: int(*(*zzz (int)) (void) );

    =====
    1) you mean: int(*(*xxx ) (int)) (void) ;
    xxx is pointer to function taking parameter int and returning...
    a pointer to function taking parameter void and returning int.

    static int count= 0;
    int dec_count(void) { return count--; }
    int inc_count(void) { return count++; }
    typedef int (* count_func) (void);
    count_func selector(int k) { return k ? inc_count : dec_count; }

    you can then make xxx point to function selector:
    int(*(*xxx ) (int)) (void)= selector;
    or...
    count_func (*xxx) (int)= selector;

    =====
    2) you mean: int(*(*yyy (int)) ) (void) ;

    this is the same as:
    int (** yyy (int)) (void);

    yyy is a function taking parameter int and returning...
    pointer to pointer to function taking parameter void and returning int.

    int two (void) { return 2; }
    int one (void) { return 1; }
    typedef int (* func) (void);
    func one_bis= one, two_bis= two;

    then yyy can be defined as:
    int (** yyy (int k)) (void) { return k ? & two_bis : & one_bis; }
    or in a more readable way:
    func * yyy (int choice) { return k ? & two_bis : & one_bis; }

    =====
    3) you mean: int(*(*zzz (int)) (void) );
    this is the same as
    int* (*zzz (int)) (void);

    zzz is a function taking parameter int and returning...
    pointer to function taking parameter void and returning int*.

    int* null (void) { return NULL; }
    int* nonnull (void) { static int hell= 666; return & hell; }
    typedef int* (* func) (void);

    then zzz can be defined as:
    int* (*zzz (int)) (void) { return k ? nonnull : null; }
    or in a more readable way:
    func zzz (int k) { return k ? nonnull : null; }

    --
    regis
    regis, Jun 24, 2007
    #4
  5. Shraddha

    regis Guest

    regis wrote:

    > yyy can be defined as:
    > int (** yyy (int k)) (void) { return k ? & two_bis : & one_bis; }
    > or in a more readable way:
    > func * yyy (int choice) { return k ? & two_bis : & one_bis; }


    Here, I meant:
    func * yyy (int k) { return k ? & two_bis : & one_bis; }


    > zzz can be defined as:
    > int* (*zzz (int)) (void) { return k ? nonnull : null; }
    > or in a more readable way:
    > func zzz (int k) { return k ? nonnull : null; }


    Here, I meant:
    int* (*zzz (int k)) (void) { return k ? nonnull : null; }
    regis, Jun 24, 2007
    #5
  6. Shraddha wrote:
    > int(*(*ptr (int))(void)
    >
    > First I thought that this is the pointer to function...But I recognize
    > that the syntax iss quite different...
    > If we say that the function is taking void parameters i.e. no
    > parameters then what "(int i)" is doing there...
    >

    Check out a utility called "cdecl", but first (as suggested else-thread)
    you will have to fix the syntax of this line.
    --
    clvrmnky <mailto:>

    Direct replies will be blacklisted. Replace "spamtrap" with my name to
    contact me directly.
    Clever Monkey, Jun 25, 2007
    #6
  7. Shraddha

    user923005 Guest

    On Jun 25, 4:37 am, Clever Monkey <>
    wrote:
    > Shraddha wrote:
    > > int(*(*ptr (int))(void)

    >
    > > First I thought that this is the pointer to function...But I recognize
    > > that the syntax iss quite different...
    > > If we say that the function is taking void parameters i.e. no
    > > parameters then what "(int i)" is doing there...

    >
    > Check out a utility called "cdecl", but first (as suggested else-thread)
    > you will have to fix the syntax of this line.
    > --
    > clvrmnky <mailto:>
    >
    > Direct replies will be blacklisted. Replace "spamtrap" with my name to
    > contact me directly.


    C:\cdecl>type test.txt
    explain int(*(*xxx ) (int)) (void) ;
    explain int(*(*yyy (int)) ) (void) ;
    explain int(*(*zzz (int)) (void) );


    C:\cdecl>cdecl < test.txt
    declare xxx as pointer to function (int) returning pointer to function
    (void) returning int
    declare yyy as function (int) returning pointer to pointer to function
    (void) returning int
    declare zzz as function (int) returning pointer to function (void)
    returning pointer to int
    user923005, Jun 25, 2007
    #7
    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.

Share This Page