Pointer to an array of pointers to functions 
Author Message
 Pointer to an array of pointers to functions

Hello All,
        I'm trying to create a data structure that contains a pointer
to an array of function pointers.  I've tried several things to
declare the array, but I'm either not sure if I got what I wanted, or
I got compiler diagnostics.

Is this declaration correct?:

        void *(*pf[ ]) (void);

Then to de-reference (that is call one of the functions):

        *pf[0] ();

Thanks in advance for the help.

Guy

--

Guy Berthiaume            
Sr. Engineer
Cirque Networks, Inc.
http://www.*-*-*.com/

Remove "piss.off.spammers." to reply by e-mail



Sun, 18 Nov 2001 03:00:00 GMT  
 Pointer to an array of pointers to functions


:       I'm trying to create a data structure that contains a pointer
: to an array of function pointers.

: Is this declaration correct?:
:
:       void *(*pf[ ]) (void);

        void (*((*pf)[]))(void);

: Then to de-reference (that is call one of the functions):
:
:       *pf[0] ();

        (*pf)[0]();

For example

  #include <stdio.h>

  void f1(void) {
    printf( "f1\n" );
  }

  void f2(void) {
    printf( "f2\n" );
  }

  int main(void) {
    void (*(f[]))(void) = {f1, f2};
    void (*((*pf)[]))(void) = &f;
    (*pf)[0]();
    (*pf)[1]();
    return 0;
  }

If in doubt, or to improve readability, use typedefs

  int main(void) {
    typedef void (*pfn)(void); /* pointer to function            */
    typedef pfn apfn[];        /* array of pointer to function   */
    typedef apfn *papfn;       /* pointer to array of pointer to */
                               /* function                       */
    apfn f = {f1, f2};
    papfn pf = &f;
    (*pf)[0]();
    (*pf)[1]();
    return 0;
  }

or use a program like cdecl to generate the declarations for you.

But are you sure you really need to use a pointer to an array? A
pointer to the first element of the array will often suffice.

--

Mathew Hendry, Programmer, Visual Sciences Ltd.

IRC/ICQ/Q2/... "Scampi", ICQ 24839718
PGP B3C1 1EBC 2BAE 93F0 54C1 E0E7 5FA3 5988 BBD6 B689



Sun, 18 Nov 2001 03:00:00 GMT  
 Pointer to an array of pointers to functions

Quote:

>    I'm trying to create a data structure that contains a pointer
>to an array of function pointers.

Actually, I would bet this is *not* (quite) what you want.

This is one of those cases where it is important to distinguish
between:

        array N of T

(for some integer constant N and valid element type T) and:

        pointer to T, pointing to the first element of an array N of T,
        i.e., the one with subscript 0

Remember that an actual object of type "array N of T" "decays" into
a value of type "pointer to T" in most contexts.  Thus, if you want
a pointer that can point to the first element of this array, or to
any element within this array, you need an object of type "pointer
to T", *not* "pointer to array N of T".

So, if your array (named "A1", perhaps) has type:

        array 10 of pointer to function (of void) returning void

or:

        void (*A1[10])(void);

then a pointer that can point to its first element must have type:

        pointer to function (of void) returning void

or:

        void (*pf1)(void);

and it can be set with:

        pf1 = A1;
or:     pf1 = &A1[0];

On the other hand, if your array (A2?) has type:

        array 5 of array 8 of pointer to function (of void) returning void

or:

        void (*A2[5][8])(void);

then a pointer to its first element must have type:

        pointer to array 8 of pointer to function (of void) returning void

(i.e., replace "array N of" with "pointer to"), or:

        void (*(*pf2)[8])(void);

which can be set with:

        pf2 = A2;
or:     pf2 = &A2[0];

In this kind of case, people often resort to typedefs (which I
was just decrying earlier :-) but at least here they are not just
being pasted over "struct"s) to eliminate some of the parentheses.
(I am not going to supply examples, as I expect others will have
done so by now.)

Quote:
>Then to de-reference (that is call one of the functions):
>    *pf[0] ();

If "pf3" has type:

        pointer to function (of args) returning T

then a call to the function can be written most simply as:

        val = pf3(args);

In K&R (pre-ANSI) C, some compilers required following the pointer
first, to obtain the function:

        val = (*pf3)(args);

and many people still prefer to write it this way.  ANSI C has
the peculiarity that the function immediately re-decays back into
another pointer, so that any number of prefix "*"s work:

        val = (***********pf3)(args);

In the case of "pf2", however, it points to the first element of
an array (of size 8) of pointers to functions (of void, returning
void), so you would have to write:

        pf2[i]();

or (K&R):

        (*pf2[i])();

Since "prefix *" can be always converted to "suffix [0]" (that is,
*(p) and (p)[0] always "mean" the same thing), the "pf1" version
can be written as:

        pf1();

(no arguments, no return value), or:

        (*pf1)();

(K&R style); or

        pf1[0]();

("odd-ified" style -- gcc generates a warning here, even though no
diagnostic is required), or even

        pf1[0][0][0][0][0][0][0]();

(where gcc emits one warning per "[0]").
--
In-Real-Life: Chris Torek, Berkeley Software Design Inc




Sun, 18 Nov 2001 03:00:00 GMT  
 Pointer to an array of pointers to functions
Hello again,
        Thanks for your responses, you led me in the right direction.
What finally worked was to declare a pointer to a pointer to a
function, then treat it as an array pointers to functions.  It works
just fine.  Thanks again.

Guy
--

Guy Berthiaume            
Sr. Engineer
Cirque Networks, Inc.
http://www.cirque-networks.com

Remove "piss.off.spammers." to reply by e-mail



Sun, 18 Nov 2001 03:00:00 GMT  
 Pointer to an array of pointers to functions

Quote:



> >       I'm trying to create a data structure that contains a pointer
> >to an array of function pointers.

> Actually, I would bet this is *not* (quite) what you want.

[...]

Quote:

> So, if your array (named "A1", perhaps) has type:

>         array 10 of pointer to function (of void) returning void

> or:

>         void (*A1[10])(void);

> then a pointer that can point to its first element must have type:

>         pointer to function (of void) returning void

make that: pointer to pointer to function (of void) returning void

Quote:

> or:

>         void (*pf1)(void);

rather: void (**pf1)(void)

Quote:

> and it can be set with:

>         pf1 = A1;
> or:     pf1 = &A1[0];

[...]

Quote:
> Since "prefix *" can be always converted to "suffix [0]" (that is,
> *(p) and (p)[0] always "mean" the same thing), the "pf1" version
> can be written as:

I don't believe that's true for pointers to functions: see below.

Quote:

>         pf1();

> (no arguments, no return value), or:

>         (*pf1)();

> (K&R style); or

>         pf1[0]();

> ("odd-ified" style -- gcc generates a warning here, even though no
> diagnostic is required), or even

This is what the C9X draft standard says: (Is the current standard
different on this subject?)

    6.5.2.1 Array subscripting
    Constraints
    1 One of the expressions shall have type pointer to object type,
     the other expression shall have integer type, and the result has
     type type.

We find a similar constraint in the section on the additive operator.

Since 'pf1' is not a "pointer to object type", it violates a constraint
and requires a diagnostic. It would be correct if "pf1" was in fact a
"pointer to pointer to function", but then the first example (pf1())
would be an error.

Quote:

>         pf1[0][0][0][0][0][0][0]();

> (where gcc emits one warning per "[0]").

--
Joe


Mon, 19 Nov 2001 03:00:00 GMT  
 Pointer to an array of pointers to functions


Quote:

> This is what the C9X draft standard says: (Is the current standard
> different on this subject?)

Sorry Joe, no idea. But I do have a sort-of-related question:

http://wwwold.dkuug.dk/jtc1/sc22/open/n2794/ is the URL I have for the C9X
draft standard, as at 3 August 1998. Is there a later draft out there
anywhere?

TIA

--
Richard Heathfield

The bug stops here.



Mon, 19 Nov 2001 03:00:00 GMT  
 Pointer to an array of pointers to functions


Quote:

> http://wwwold.dkuug.dk/jtc1/sc22/open/n2794/ is the URL I have for the
C9X
> draft standard, as at 3 August 1998. Is there a later draft out there
> anywhere?

> TIA

> --
> Richard Heathfield

> The bug stops here.

   http://anubis.dkuug.dk/JTC1/SC22/WG14/www/docs/n869/
      ( Draft dated 18 January 1999)

--
        Regards,
                Alex Krol
Disclaimer: I'm not speaking for Scitex Corporation Ltd

Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.



Mon, 19 Nov 2001 03:00:00 GMT  
 Pointer to an array of pointers to functions


Quote:


>> This is what the C9X draft standard says: (Is the current standard
>> different on this subject?)

>Sorry Joe, no idea. But I do have a sort-of-related question:

>http://wwwold.dkuug.dk/jtc1/sc22/open/n2794/ is the URL I have for the C9X
>draft standard, as at 3 August 1998. Is there a later draft out there

http://www.dkuug.dk/jtc1/sc22/wg14/www/docs/n869/

This is a directory that contains n869 (dated 8th January 1999) in various
forms.

--
-----------------------------------------


-----------------------------------------



Mon, 19 Nov 2001 03:00:00 GMT  
 Pointer to an array of pointers to functions


supplied corrections.

I must have been suffering brain fade (was doing news in between
SMP kernel debugging).

I can supply one additional note.

[me]

Quote:
>>Since "prefix *" can be always converted to "suffix [0]" (that is,
>>*(p) and (p)[0] always "mean" the same thing) ...

[Joe Maun]

Quote:
>I don't believe that's true for pointers to functions ...
>This is what the C9X draft standard says: (Is the current standard
>different on this subject?)

>    6.5.2.1 Array subscripting
>    Constraints
>    1 One of the expressions shall have type ``pointer to object type'',
>     the other expression shall have integer type, and the result has
>     type ``type''.

The original 1989 standard uses the same wording.

All my pointers had one too few levels of indirection as well (as
Joe Maun noted).  Sorry about that.
--
In-Real-Life: Chris Torek, Berkeley Software Design Inc




Mon, 19 Nov 2001 03:00:00 GMT  
 Pointer to an array of pointers to functions


Quote:


> >http://wwwold.dkuug.dk/jtc1/sc22/open/n2794/ is the URL I have for the
C9X
> >draft standard, as at 3 August 1998. Is there a later draft out there

> http://www.dkuug.dk/jtc1/sc22/wg14/www/docs/n869/

> This is a directory that contains n869 (dated 8th January 1999) in
various
> forms.

Thanks Lawrence (and Alex).

Next question - where can I find a Windows gz decompressor? :-)

(It's okay, I know, don't answer that, it's way off-topic... I guess I'll
just have to go buy a monitor for my Linux box instead...)

--
Richard Heathfield

The bug stops here.



Mon, 19 Nov 2001 03:00:00 GMT  
 Pointer to an array of pointers to functions

Quote:

>Next question - where can I find a Windows gz decompressor? :-)

>(It's okay, I know, don't answer that, it's way off-topic... I guess I'll
>just have to go buy a monitor for my Linux box instead...)

Last time I checked, WinZip was able to extract gzipped files. I think.
Also, you could telnet into the Linux box, gunzip the file, and FTP it
back (that's assuming your computers are in a network).

Gergo

--
To err is human, to moo bovine.

GU d- s:+ a--- C++>$ UL+++ P>++ L+++ E>++ W+ N++ o? K- w--- !O !M !V
PS+ PE+ Y+ PGP+ t* 5+ X- R>+ tv++ b+>+++ DI+ D+ G>++ e* h! !r !y+



Mon, 19 Nov 2001 03:00:00 GMT  
 
 [ 16 post ]  Go to page: [1] [2]

 Relevant Pages 

1. How to use (pointer to function), and function and pointer to (pointer to function)

2. function pointers and function pointers array

3. Dereferencing f-pointers, arrays of f-pointers, pointers to f-pointers

4. Pointer Functions and Pointers to Pointer Functions

5. Pointer to an array of function pointers [HELP!]

6. Pointer of Pointers was Pointer of arrays...

7. Pointers: return of pointer to array of pointers to main

8. Array of pointers, pointer to array...

9. array pointer/pointer array

10. arrays pointers array of pointers

11. Question about signal()/pointers to functions that return pointers to functions

12. C++ function pointers versus C function pointer problem

 

 
Powered by phpBB® Forum Software