Pointer to an array of function pointers [HELP!] 
Author Message
 Pointer to an array of function pointers [HELP!]

As an argument to function LOAD_POINTERS, I need to pass a pointer
to a (constant) array of function pointers. (All the functions
pointed to in the array return void and take void arguments.)

The function's purpose is to load a variable array of function
pointers with the contents of the constant array.

Can anyone help me with this one? Specifically, how do I
write LOAD_POINTERS, and how do I call it -- ie, how do I
specify the pointer argument to be passed.

Thanks for any help, Doug Manzer



Fri, 05 Feb 1999 03:00:00 GMT  
 Pointer to an array of function pointers [HELP!]

Quote:

> As an argument to function LOAD_POINTERS, I need to pass a pointer
> to a (constant) array of function pointers. (All the functions
> pointed to in the array return void and take void arguments.)

> The function's purpose is to load a variable array of function
> pointers with the contents of the constant array.

> Can anyone help me with this one? Specifically, how do I
> write LOAD_POINTERS, and how do I call it -- ie, how do I
> specify the pointer argument to be passed.

> Thanks for any help, Doug Manze

The easiest way to do it is to define a new type (lets call it fcts).

typedef void (*fcts)(void);

then, using this type, you can declare an array.

fcts myfcts[5];

To initialize the array just use a single assignation on each pointers.

myfcts[0] = function1;  <--- No Parantheses

where function1 is defined as:

        void function1(void);

To call a function:

        myfcts[0]();

Once this done you can call your LOAD_POINTERS function.

LOAD_FUNCTIONS(myfcts);

which should be defined:

        type LOAD_FUNCTIONS(fcts fctpointers[]);

So your code should look like this:
---------------------------------------------------

#include <stdio.h>

typedef void (*fcts)(void);

void f1(void) { printf("1\n"); }
void f2(void) { printf("2\n"); }
void f3(void) { printf("3\n"); }

void myprintf(fcts fctspointers[])
{
    int i;

    for (i=0; i<3; i++)
      fctspointers[i]();

Quote:
}

int main()
{
    fcts myfcts[3];

    myfcts[0] = f1;
    myfcts[1] = f2;
    myfcts[2] = f3;

    myprintf(myfcts);

    return 0;

Quote:
}

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

Hope that helps!

--
                              ,,,
                             (o o)
-------------------------oOO--(_)--OOo---------------------------------
                                 |
     _/     _/_/_/   _/_/_/      |  Jean-Sebastien Vachon
    _/    _/       _/            |  ACE Software Engineer, LSC 2+1
INC.  
   _/     _/_/_/  _/             |                              

 _/_/_/ _/_/_/   _/_/_/ 2+1 INC. |                              
                                 |  ACE Brings life to your Data
---------------------------(())--(())----------------------------------



Sun, 07 Feb 1999 03:00:00 GMT  
 Pointer to an array of function pointers [HELP!]

Quote:

> As an argument to function LOAD_POINTERS, I need to pass a pointer
> to a (constant) array of function pointers. (All the functions
> pointed to in the array return void and take void arguments.)

> The function's purpose is to load a variable array of function
> pointers with the contents of the constant array.

> Can anyone help me with this one? Specifically, how do I
> write LOAD_POINTERS, and how do I call it -- ie, how do I
> specify the pointer argument to be passed.

> Thanks for any help, Doug Manzer

/* this help for understand */
typedef void (*function_ptr)(void);

/* some dummy functions for test */
void a(void){}
void b(void){}
void c(void){}
void d(void){}

/* the following may bug with some compilers that do not like
 * to initialize global var with addresses of other global vars */
const function_ptr source[5] = { &a, &b, &c, &d, NULL };

function_ptr *target; /* the array to fill */

void load_pointers(const function_ptr * const s,
                         function_ptr *       t )
{ /* this function copy the s array to the t array */
  int i;

  for (i = 0; s[i]; i++) t[i] = s[i];
  t[i] = NULL;

Quote:
}

int main(...)
{
  ...
  function_ptr *target; /* the array to fill */

  target = malloc( 5 * sizeof(function_ptr));
  load_pointers(source, target);
  ...

Quote:
}



Tue, 09 Feb 1999 03:00:00 GMT  
 Pointer to an array of function pointers [HELP!]


<snip>
FA: /* the following may bug with some compilers that do not like
FA:  * to initialize global var with addresses of other global vars */
FA: const function_ptr source[5] = { &a, &b, &c, &d, NULL };

C as currently defined allows it: so compilers which fail to compile
this are not conforming.

FA: void load_pointers(const function_ptr * const s,
FA:                          function_ptr *       t )

Why not `function_ptr *const  t'. Inconsistency makes everything more
difficult to understand.

FA: { /* this function copy the s array to the t array */
FA:   int i;
FA:
FA:   for (i = 0; s[i]; i++) t[i] = s[i];
FA:   t[i] = NULL;
FA: }

Cheers
Tanmoy
--

Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87545 H:#9,3000,Trinity Drive,NM87544
Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
<http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
fax: 1 (505) 665 3003   voice: 1 (505) 665 4733    [ Home: 1 (505) 662 5596 ]



Tue, 09 Feb 1999 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. function pointers and function pointers array

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

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 pointers to functions

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. Function pointer array help needed

12. Help - Class Member Function Array Pointers

 

 
Powered by phpBB® Forum Software