0

I was trying to create an array of function pointers with the following piece of code:

#include <stdio.h>

int functu(int ,int);
int ghonchu(int,int);

int main()
{
    printf("Hello, World!\n");

    int (*acrib[10]) (int,int);

    acrib[0] = (*functu)(int,int);
    return 0;
}

On compiling this program, an error is thrown saying "too few arguments to function 'functu'".

What can be the cause of it? Am I missing anything trivial?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
dead poet
  • 23
  • 5

2 Answers2

4

You seem to be confusing & (which returns a pointer) with * (which dereferences a pointer).

You need to change this:

   acrib[0] = (*functu)(int,int);

to this:

   acrib[0] = &functu;

See: http://ideone.com/r1Xqa6

ruakh
  • 175,680
  • 26
  • 273
  • 307
  • Got it ! Thanks a lot. – dead poet Mar 19 '17 at 23:07
  • Problem still persists. Sorry – dead poet Mar 19 '17 at 23:12
  • @deadpoet: I've added an Ideone.com link. – ruakh Mar 19 '17 at 23:37
  • The `&` is entirely optional; the code would work correctly with `acrib[0] = functu;`. In fact, it would also work with `acrib[0] = *functu;` or even (horror of horrors) `acrib[0] = **functu;` — the key point is that the function-invoking left parenthesis is missing. Personally, I skip the `&`, but I understand the desire to use it. I'd never use the `*` notation when initializing a function pointer, but I would usually write `(*acrib[0])(10, 20);` rather than `acrib[0](10, 20);`, though that is in part because the latter notation wasn't an option when I learned C. – Jonathan Leffler Mar 20 '17 at 00:08
  • 1
    @JonathanLeffler: That's a good point. I guess it would be more correct to say that the OP was confusing the syntax for declaring a function pointer with the syntax for referring to a function pointer. – ruakh Mar 20 '17 at 02:38
0

You should have function implementation, then assign the implementation to the pointer before using it. short example

Community
  • 1
  • 1
drqCode
  • 47
  • 1
  • 7