complex pointer declaration

Discussion in 'C Programming' started by junky_fellow@yahoo.co.in, Sep 19, 2007.

  1. Guest

    Guys,

    I am sorry for asking so many questions.

    I need to declare a pointer to an array of pointer to functions
    where each function takes void as an argument and returns int.

    I could declare an array of pointers to functions, but not the
    complete thing.

    I declared the,
    array of pointers to functions (where each functions takes void as
    argument and returns int) as follows:
    int (*ptr [] ) (void); // Hope this is correct

    Please help me to declare a pointer to above type.

    Again many thanks for any help.....
    , Sep 19, 2007
    #1
    1. Advertising

  2. said:

    > Guys,
    >
    > I am sorry for asking so many questions.
    >
    > I need to declare a pointer to an array of pointer to functions
    > where each function takes void as an argument and returns int.


    typedef is your friend. Start off by setting up a synonym for the function
    type:

    typedef int junkyfunc(void);

    Now let's have an array of pointers:

    #define N 32 /* or whatever */

    typedef junkyfunc *array_of_N_jf_ptrs[N];

    Now we can easily define a pointer to such an array:

    array_of_N_jf_ptrs *p;

    --
    Richard Heathfield <http://www.cpax.org.uk>
    Email: -www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999
    Richard Heathfield, Sep 19, 2007
    #2
    1. Advertising

  3. Mark Bluemel Guest

    Richard Heathfield wrote:
    > said:
    >
    >> Guys,
    >>
    >> I am sorry for asking so many questions.
    >>
    >> I need to declare a pointer to an array of pointer to functions
    >> where each function takes void as an argument and returns int.

    >
    > typedef is your friend. Start off by setting up a synonym for the function
    > type:
    >
    > typedef int junkyfunc(void);
    >
    > Now let's have an array of pointers:
    >
    > #define N 32 /* or whatever */
    >
    > typedef junkyfunc *array_of_N_jf_ptrs[N];
    >
    > Now we can easily define a pointer to such an array:
    >
    > array_of_N_jf_ptrs *p;
    >


    Hmmm. Is it actually necessary, in most cases, to distinguish between a
    a pointer to an array of such functions and a pointer to the first
    function in such an array?
    Mark Bluemel, Sep 19, 2007
    #3
  4. Guest

    On Sep 19, 6:29 pm, Richard Heathfield <> wrote:
    > said:
    >
    > > Guys,

    >
    > > I am sorry for asking so many questions.

    >
    > > I need to declare a pointer to an array of pointer to functions
    > > where each function takes void as an argument and returns int.

    >
    > typedef is your friend. Start off by setting up a synonym for the function
    > type:
    >
    > typedef int junkyfunc(void);
    >
    > Now let's have an array of pointers:
    >
    > #define N 32 /* or whatever */
    >
    > typedef junkyfunc *array_of_N_jf_ptrs[N];
    >
    > Now we can easily define a pointer to such an array:
    >
    > array_of_N_jf_ptrs *p;
    >

    Thanks Richard. This is really a nice way of declaring complex types.
    , Sep 19, 2007
    #4
  5. Guest

    On Sep 19, 6:45 pm, Mark Bluemel <> wrote:
    > Richard Heathfield wrote:
    > > said:

    >
    > >> Guys,

    >
    > >> I am sorry for asking so many questions.

    >
    > >> I need to declare a pointer to an array of pointer to functions
    > >> where each function takes void as an argument and returns int.

    >
    > > typedef is your friend. Start off by setting up a synonym for the function
    > > type:

    >
    > > typedef int junkyfunc(void);

    >
    > > Now let's have an array of pointers:

    >
    > > #define N 32 /* or whatever */

    >
    > > typedef junkyfunc *array_of_N_jf_ptrs[N];

    >
    > > Now we can easily define a pointer to such an array:

    >
    > > array_of_N_jf_ptrs *p;

    >
    > Hmmm. Is it actually necessary, in most cases, to distinguish between a
    > a pointer to an array of such functions and a pointer to the first
    > function in such an array?- Hide quoted text -
    >


    Actually, I need to find the number of elements in the array. So, I
    must know its type. By having a pointer to first element, I cannot do
    that.
    , Sep 19, 2007
    #5
  6. Mark Bluemel <> writes:
    > Richard Heathfield wrote:
    >> said:
    >>> I need to declare a pointer to an array of pointer to functions
    >>> where each function takes void as an argument and returns int.

    >> typedef is your friend. Start off by setting up a synonym for the
    >> function type:
    >> typedef int junkyfunc(void);
    >> Now let's have an array of pointers:
    >> #define N 32 /* or whatever */
    >> typedef junkyfunc *array_of_N_jf_ptrs[N];
    >> Now we can easily define a pointer to such an array:
    >> array_of_N_jf_ptrs *p;

    >
    > Hmmm. Is it actually necessary, in most cases, to distinguish between
    > a a pointer to an array of such functions and a pointer to the first
    > function in such an array?


    Yes, it's necessary to distinguish between a pointer to an array and a
    pointer to the first element of an array. The main difference is that
    pointers to arrays are rarely useful.

    --
    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, Sep 19, 2007
    #6
  7. "" <> writes:
    > On Sep 19, 6:45 pm, Mark Bluemel <> wrote:

    [...]
    >> Hmmm. Is it actually necessary, in most cases, to distinguish between a
    >> a pointer to an array of such functions and a pointer to the first
    >> function in such an array?

    >
    > Actually, I need to find the number of elements in the array. So, I
    > must know its type. By having a pointer to first element, I cannot do
    > that.


    If you declare a pointer to an array, the size of the array must be
    fixed at compilation time (unless you're using VLAs, which aren't
    universally supported).

    Pointers to arrays are rarely as useful as pointers to the first
    element of an array. You can use ordinary indexing to access all the
    elements of the array. You just have to keep track of the size
    separately.

    --
    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, Sep 19, 2007
    #7
  8. CBFalconer Guest

    "" wrote:
    > Mark Bluemel <> wrote:
    >> Richard Heathfield wrote:
    >>> said:

    >>
    >>>> I need to declare a pointer to an array of pointer to functions
    >>>> where each function takes void as an argument and returns int.

    >>
    >>> typedef is your friend. Start off by setting up a synonym for
    >>> the function type:

    >>
    >>> typedef int junkyfunc(void);

    >>
    >>> Now let's have an array of pointers:

    >>
    >>> #define N 32 /* or whatever */
    >>> typedef junkyfunc *array_of_N_jf_ptrs[N];

    >>
    >>> Now we can easily define a pointer to such an array:

    >>
    >>> array_of_N_jf_ptrs *p;

    >>
    >> Hmmm. Is it actually necessary, in most cases, to distinguish
    >> between a a pointer to an array of such functions and a pointer
    >> to the first function in such an array?

    >
    > Actually, I need to find the number of elements in the array. So,
    > I must know its type. By having a pointer to first element, I
    > cannot do that.


    Sure you can. When you allocate memory to p with:
    p = malloc(N * sizeof *p;
    just remember the value of N. If you define a local variable for
    the array, with:
    arr_of_N_jf_ptrs arr[N];
    you can either retain N or recover the value (in the same scope)
    with "(sizeof arr)/sizeof arr[0])".

    --
    Chuck F (cbfalconer at maineline dot net)
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net>



    --
    Posted via a free Usenet account from http://www.teranews.com
    CBFalconer, Sep 20, 2007
    #8
  9. Mark Bluemel Guest

    Keith Thompson wrote:
    > Mark Bluemel <> writes:


    >> Hmmm. Is it actually necessary, in most cases, to distinguish between
    >> a a pointer to an array of such functions and a pointer to the first
    >> function in such an array?

    >
    > Yes, it's necessary to distinguish between a pointer to an array and a
    > pointer to the first element of an array. The main difference is that
    > pointers to arrays are rarely useful.
    >

    Yes. I think that was what I was trying to express - but failed...
    Mark Bluemel, Sep 20, 2007
    #9
  10. John Bode Guest

    On Wed, 19 Sep 2007 06:21:30 -0700, wrote:

    > Guys,
    >
    > I am sorry for asking so many questions.
    >
    > I need to declare a pointer to an array of pointer to functions
    > where each function takes void as an argument and returns int.
    >


    f -- f
    *f -- is a pointer
    (*f)[N] -- to an N-element array
    *(*f)[N] -- of pointer
    (*(*f)[N])() -- to function
    (*(*f)[N])(void) -- taking void
    int (*(*f)[N])(void) -- and returning int

    Hope that helps. Typedefs would make it more readable; naked pointers are
    a bit ugly.
    John Bode, Sep 21, 2007
    #10
    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. news.amnet.net.au
    Replies:
    1
    Views:
    559
    =?UTF-8?b?TMSByrtpZSBUZWNoaWU=?=
    Apr 13, 2004
  2. Stanimir Stamenkov
    Replies:
    2
    Views:
    734
    Stanimir Stamenkov
    Oct 25, 2005
  3. Robert Mark Bram
    Replies:
    0
    Views:
    673
    Robert Mark Bram
    Feb 4, 2007
  4. Bolin
    Replies:
    4
    Views:
    401
  5. Kottiyath

    How complex is complex?

    Kottiyath, Mar 18, 2009, in forum: Python
    Replies:
    22
    Views:
    739
Loading...

Share This Page