1

I am trying to use function pointer, which is somewhat like this:

#include "stdio.h"

typedef void (*func)(int*, int*);


void func1(int *a, int*b)
{
    printf("Func1\n");
}

void func2(int *a, int*b)
{
    printf("Func2\n");
}

void func3(int *a, int*b)
{
    printf("Func3\n");
}

int main()
{
    int i;
    func f[] = {
      func1, func2, func3
    };
    printf("Hello\n");

    for(i=0; i< 3; i++)
    {
        func fn = f[i];
        *(fn)(&i, &i);
    }
    return 0;
}

I am always getting error: "void value not ignored as it ought to be"

Do not know, how to overcome this. Can anybody please help?

5 Answers 5

1

You need to use like following :

(*fn)(&i, &i);

Sign up to request clarification or add additional context in comments.

Comments

1

It's either (*fn)(&i, &i), or just fn(&i,&i). Otherwise you're trying to dereference the result of the function call!

(Alternatively, five-star-programmers often say (*****fn)(&i, &i).

Comments

0

This error means you assign the return of a void function to something, which is an error.

Try this:-

(*fn)(&i, &i);

Comments

0

Try this:

(*fn)(&i, &i);

Comments

0

Don't write *(fn)(...) - simple fn(...) is enough, no need to dereference anything. What you have means something like "Dereference the return value of fn(...)", but you obviously can't do that.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.