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


Groups > comp.lang.c > #108072

Re: correct way to create dynamically function pointers

From Richard Heathfield <rjh@cpax.org.uk>
Newsgroups comp.lang.c
Subject Re: correct way to create dynamically function pointers
Date 2017-04-12 08:19 +0100
Organization Fix this later
Message-ID <ockk7b$dkn$1@dont-email.me> (permalink)
References <b6cbce81-b326-4c26-9079-23e083b2de56@googlegroups.com>

Show all headers | View raw


On 11/04/17 18:20, Hello World wrote:
> Hi all,
>
> 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

Okay, so presumably you effectively want an array of function pointers. 
(Otherwise, why bother with dynamic allocation?)

> #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);
>
> int (*p)(int a, int b);

I'm going to use a typedef here, just to keep things tidy (and later on, 
I'll take it out again):

typedef int ft_arithmetic(int, int);

This means: "ft_arithmetic is now a synonym for the /type/ of a function 
that takes two ints and returns int".

We could, in theory, now do this:

ft_arithmetic sum, subt, mul, dive;

but of course we're not going to.

The other things I'm going to do are:

* remove p from file scope. It has no business being there;
* fix p's type;
* fix the malloc argument, which is broken.

> int sum(int a, int b) { return a+b; }

(One-lined only for Usenet brevity, not out of any kind of stylistic 
recommendation!)

> int subtract(int a, int b) { return a-b; }

You prototyped this as 'subt', and you identify it by 'subt' in main(), 
so I presume you meant 'subt', not 'subtract'.

> 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);

Okay, there are several problems here. First, p has the wrong type, and 
secondly, the allocation is wrong.

Note that we could simply write:

         ft_arithmetic *p[4] = { sum, subt, mul, dive };

But of course you want /dynamic/ allocation. So we want:

         ft_arithmetic **p = malloc(4 * sizeof *p);

Translation: p is a pointer. To what /type/ does it point? Let's analogise:

char * ............ points to char
char ** ........... points to char *
int * ............. points to int
int ** ............ points to int *
ft_arithmetic * ... points to ft_arithmetic
ft_arithmetic ** .. points to ft_arithmetic *

So p is a pointer, and the kinds of things it points to are themselves 
pointers (function pointers).

sizeof *p therefore calculates the number of bytes we need for storing a 
pointer to a function of type int(int, int), and 4 * sizeof *p therefore 
gives us the number of bytes we need for storing four such pointers.

The rest of your code was fine.

Here is the code with the typedef in place:

#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);

typedef int ft_arithmetic(int, int);

int sum(int a, int b) { return a+b; }
int subt(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)
{
   ft_arithmetic **p = malloc(4 * sizeof *p);
   if(p != NULL)
   {
     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;
}

And here it is again, without the typedef, in case you're the kind of 
person who doesn't like typedefs:

#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);

int sum(int a, int b) { return a+b; }
int subt(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)
{
   int (**p)(int, int) = malloc(4 * sizeof *p);
   if(p != NULL)
   {
     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;
}


-- 
Richard Heathfield
Email: rjh at cpax dot org dot uk
"Usenet is a strange place" - dmr 29 July 1999
Sig line 4 vacant - apply within

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