Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #108061

Re: correct way to create dynamically function pointers

From Ben Bacarisse <ben.usenet@bsb.me.uk>
Newsgroups comp.lang.c
Subject Re: correct way to create dynamically function pointers
Date 2017-04-11 19:03 +0100
Organization A noiseless patient Spider
Message-ID <8760iastx8.fsf@bsb.me.uk> (permalink)
References <b6cbce81-b326-4c26-9079-23e083b2de56@googlegroups.com>

Show all headers | View raw


Hello World <acidlategamer@gmail.com> writes:

> I am learning C but I don't understand certain topics with much clarity.
> I came to know that normally it is better to use dynamic memory
> allocation, so I tried to make a c program that will dynamically
> allocate memory for function pointers. I wrote this
>
> #include <stdio.h>
> #include <stdlib.h>
>
> int sum(int a, int b);
> int subt(int a, int b);
> int mul(int a, int b);
> int dive(int a, int b);

You don't define some of these.  In fact you don't need the declarations
at all since you refer to the function only after their definitions.

> int (*p)(int a, int b);

Eek!  A "global variable"!  No!

> int sum(int a, int b)
> {
> 	return a+b;
> }
>
> int subtract(int a, int b)
> {
> 	return a-b;
> }
>
> int mul(int a, int b)
> {
> 	return a*b;
> }
>
> int dive(int a, int b)
> {
> 	return a/b;
> }
>
> int main(void)
> {
> 	p = malloc(sizeof(p)*4);
> 	p[0] = sum;
> 	p[1] = subt;
> 	p[2] = mul;
> 	p[3] = dive;
> 	printf("p[0](4, 6) = %d\n",(*p[0])(4, 6));
> 	printf("p[1](4, 6) = %d\n",(*p[1])(4, 6));
> 	printf("p[2](4, 6) = %d\n",(*p[2])(4, 6));
> 	printf("p[3](4, 6) = %d\n",(*p[3])(4, 6));
> 	free(p);
> 	return 0;
> }
>
> }
>
> However I get an error
>
> warning: ISO C forbids assignment between function pointer and ‘void *’ [-Wpedantic]
>   p = malloc(sizeof(p)*4);
>     ^
>
> It looks like that function pointer is a different kind of pointer.
> What am I missing?

When you allocate space of something of type T you get back a pointer
that you store in a variable of type T *.  E.g. you allocate ten ints
and you put the result into a int *:

  int *ints = malloc(10 * sizeof *ints);

The same goes for 4 function pointers.  You need to take the result and
put it a variable of type pointer to function pointer.

These things are simpler with a typedef:

  typedef int operation(int, int);

Now you can make a pointer to a pointer to an "operation" function like
this:

  operation **oplist = malloc(4 * sizeof *oplist);

Some people make the typedef a pointer-to-function type, but I don't
like to hide pointer types behind typedefs and, what's more, this way
you get to declare the functions more easily too:

  operation add, subtract, multiply, divide;

-- 
Ben.

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

correct way to create dynamically function pointers Hello World <acidlategamer@gmail.com> - 2017-04-11 10:20 -0700
  Re: correct way to create dynamically function pointers scott@slp53.sl.home (Scott Lurndal) - 2017-04-11 17:34 +0000
    Re: correct way to create dynamically function pointers Hello World <acidlategamer@gmail.com> - 2017-04-11 10:42 -0700
      Re: correct way to create dynamically function pointers scott@slp53.sl.home (Scott Lurndal) - 2017-04-11 17:48 +0000
        Re: correct way to create dynamically function pointers Hello World <acidlategamer@gmail.com> - 2017-04-11 10:55 -0700
      Re: correct way to create dynamically function pointers Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-04-11 19:06 +0100
      Re: correct way to create dynamically function pointers "James R. Kuyper" <jameskuyper@verizon.net> - 2017-04-11 14:23 -0400
    Re: correct way to create dynamically function pointers Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-04-11 19:05 +0100
    Re: correct way to create dynamically function pointers "James R. Kuyper" <jameskuyper@verizon.net> - 2017-04-11 14:11 -0400
      Re: correct way to create dynamically function pointers Richard Heathfield <rjh@cpax.org.uk> - 2017-04-12 08:27 +0100
        Re: correct way to create dynamically function pointers "James R. Kuyper" <jameskuyper@verizon.net> - 2017-04-12 13:31 -0400
          Re: correct way to create dynamically function pointers Richard Heathfield <rjh@cpax.org.uk> - 2017-04-12 23:05 +0100
  Re: correct way to create dynamically function pointers Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-04-11 19:03 +0100
  Re: correct way to create dynamically function pointers "James R. Kuyper" <jameskuyper@verizon.net> - 2017-04-11 14:05 -0400
    Re: correct way to create dynamically function pointers Jorgen Grahn <grahn+nntp@snipabacken.se> - 2017-04-15 10:19 +0000
      Re: correct way to create dynamically function pointers asetofsymbols@gmail.com - 2017-04-15 05:09 -0700
  Re: correct way to create dynamically function pointers Barry Schwarz <schwarzb@dqel.com> - 2017-04-11 15:13 -0700
    Re: correct way to create dynamically function pointers Richard Heathfield <rjh@cpax.org.uk> - 2017-04-12 08:28 +0100
  Re: correct way to create dynamically function pointers Richard Heathfield <rjh@cpax.org.uk> - 2017-04-12 08:19 +0100
    Re: correct way to create dynamically function pointers Spiros Bousbouras <spibou@gmail.com> - 2017-04-12 21:17 +0000
      Re: correct way to create dynamically function pointers Richard Heathfield <rjh@cpax.org.uk> - 2017-04-12 23:11 +0100
        Re: correct way to create dynamically function pointers Keith Thompson <kst-u@mib.org> - 2017-04-12 16:36 -0700
  Re: correct way to create dynamically function pointers John Bode <jfbode1029@gmail.com> - 2017-04-12 10:04 -0700
    Re: correct way to create dynamically function pointers Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-04-12 19:28 +0100
    Re: correct way to create dynamically function pointers Tim Rentsch <txr@alumni.caltech.edu> - 2017-04-17 05:50 -0700

csiph-web