C Function Pointer Mishap - c

Okay so I'm trying to learn function pointers. I have a basic function pointer setup like so.
Function to print out linked list:
void seq_print(Seq seq, void (* print_func)(void *)){
Node * p = seq->top;
if(p == NULL){
printf("%s %c", "There is no data to print.", '\n');
return;
}
while(p != NULL){
print_func(p->data);
p = p->next;
}
}
Testing the function:
seq_print(s, printFunc(1));
I get this error:
seq.h:113:32: error: expected declaration specifiers or ‘...’ before ‘(’ token
extern void seq_print(Seq seq, (void *) print_func(void *));
I'm really not sure what to do, any insight would be helpful.

You have two mistakes:
First, notice declaration in error message: in your header file seq.h, declaration of function is wrong!
extern void seq_print(Seq seq, (void *) print_func(void *));
// ^ ^ wrong = parenthesis return type
it should be:
extern void seq_print(Seq seq, void (*print_func) (void *));
// ^ correct ^ = parenthesis function name
Second, at calling place.
seq_print(s, printFunc(1));
// ^^^ you are calling function, and passes returned value
should be:
seq_print(s, printFunc);
// ^^^^^^^^^^ don't call pass function address
My following code examples will help you to understand better (read comments):
#include<stdio.h>
void my_g2(int i, (void*) f(int)); // Mistake: Notice () around void*
void f(int i){
printf("In f() i = %d\n", i);
}
int main(){
my_g2(10, f(1)); // wrong calling
return 0;
}
void my_g2(int i, void (*f)(int)){
printf("In g()\n");
f(i);
}
Check codepad for working code. You can see error is similar to what you are getting:
Line 2: error: expected declaration specifiers or '...' before '(' token
In function 'main':
Line 8: error: too many arguments to function 'my_g2'
Now correct version of this code:
#include<stdio.h>
void my_g2(int i, void (*f)(int)); // Corrected declaration
void f(int i){
printf("In f() i = %d\n", i);
}
int main(){
my_g2(10, f); // corrected calling too
return 0;
}
void my_g2(int i, void (*f) (int)){
printf("In g()\n");
f(i);
}
Now check codepade for output:
In g()
In f() i = 10
Edit: Adding on the basis of comment.
But what if it's like void (*f) (void *) how do I pass in values to that?
From calling function in main() (in my example = my_g2) you need to pass function pointer which you wants call (in my example f()) from function you calls in main (that is my_g2).
You wanted to call f() from my_g2()
We always pass parameters to function at the time of function calling. So if you wants to pass parameters to f() function you have to pass when you call this in my_g2().
A calling expression like below (read comments):
seq_print(s, printFunc(1));
^ // first printFunc(1) will be called then seq_prints
pass returned value from printFunc(1)
is wrong because if you do so seq_print will be called with second paramter value = returned value from function printFunc(1).
To pass void pointer, my following code may help you further:
#include<stdio.h>
void my_g2(void* i, void (*f)(void*));
void f(void *i){
printf("In f(), i = %d\n", *(int*)i);
*(int*)i = 20;
}
int main(){
int i = 10;
my_g2(&i, f);
printf("Im main, i = %d", i);
return 0;
}
void my_g2(void* i, void (*f)(void*)){
printf("In g()\n");
f(i);
}
Output #codepade:
In g()
In f(), i = 10
Im main, i = 20

There's a typo in the forward declaration quoted in the error message. It needs to match the code snippet you posted, with a parameter declaration of void (* print_func)(void *).

Looks like your header has a typo. The declaration should be
extern void seq_print(Seq seq, void (*print_func)(void *));
(void *)print_func(void *) is not a valid function pointer declaration. To declare a function print_func that accepts a void pointer and does not return a value, use void (*print_func)(void *)
EDIT: omitting the parens around (*print_func) does create a function pointer but for a function returning a pointer

Related

Call a function using a pointer and pass the pointer that can point to the function along in the parameters

Say that I have a pointer to function theFunc. theFunc takes along a pointer that can point to any function with the same parameter list as theFunc, so the function called can set the passed pointer to NULL or a different function.
Using it would look like this:
while (funcPtr != NULL)
{
funcPtr(&funcPtr);
}
Would defining this be impossible?
Yes, it's doable.
The simple way:
void (*fptr_t)(void*);
The function pointer is data, even though it point to non-data. Therefore a pointer to function pointer can be converted to void* without relying on compiler extensions.
This solution lacks type safety. However, it can be improved.
Currently, it is possible to declare a function taking unspecified number of parameters. It allows to form an incomplete function type. For example:
int foo();
declares a function that returns int and takes unspecified parameters. To have a function taking no parameters use int foo(void).
This allows to declare a function taking a pointer to pointer to incomplete function type:
int foo(int (**)());
// call
int (*fptr)(int (**)()) = foo;
fptr(&fptr);
As mentioned in other answers typedef-ing function types makes the code cleaner.
typedef int foo_aux_f();
typedef int foo_f(foo_aux_f**);
foo_f *fptr = &foo;
fptr(&fptr);
It is possible to improve type safety by nesting the declaration of function types deeper and deeper.
typedef int foo_aux0_f();
typedef int foo_aux1_f(foo_aux0_f**);
typedef int foo_aux2_f(foo_aux1_f**);
typedef int foo_aux3_f(foo_aux2_f**);
typedef int foo_f(foo_aux3_f**);
foo_f fptr = &foo;
fptr(&fptr);
The perfect recursive type would be reached with infinite chain of declaration but in practice 2-3 levels are sufficient.
With some abuse of the syntax of typedef keyword it is possible to squeeze the declaration of this type:
typedef int foo_aux0_f(),
foo_aux1_f(foo_aux0_f**),
foo_aux2_f(foo_aux1_f**),
foo_aux3_f(foo_aux2_f**),
foo_f(foo_aux3_f**);
Unfortunately ... or fortunately, this trick will likely not work in upcoming C23 because the old function declarations without prototypes are planned to be removed from the language making () mean no arguments rather then unspecified number of argument.
Yes, you can pass pointer to pointer to function. The syntax is much easier if you use typedefs.
typedef void somefunc(void);
void func1(void)
{
printf("Func1\r");
}
void func2(void)
{
printf("Func2\r");
}
void swapfunction(somefunc **ptr)
{
if(*ptr == func1) *ptr = func2;
else *ptr = func1;
}
int main(void)
{
somefunc *ptr = NULL;
swapfunction(&ptr);
ptr();
swapfunction(&ptr);
ptr();
}
You can also use function return value:
typedef void somefunc(void);
void func1(void)
{
printf("Func1\r");
}
void func2(void)
{
printf("Func2\r");
}
somefunc *swapfunction(somefunc *ptr)
{
if(!ptr) return func1;
else if (ptr == func1) return func2;
else return NULL;
}
int main(void)
{
somefunc *ptr = NULL;
while(ptr = swapfunction(ptr))
{
ptr();
}
}
Ref your github comment, suggest you use a structure instead of type casting pointers to function pointers, etc. It's not exactly what you are requesting, but kind of.
The code will then look like:
#include <stdio.h>
struct funcArgStruct
{
void (*state)(struct funcArgStruct *);
// int extra_data; // optional
};
typedef struct funcArgStruct funcArg;
void start (funcArg *ptr);
void task1 (funcArg *ptr);
void stop (funcArg *ptr);
/* Implementation of an fsm. */
int main()
{
funcArg ptr_, *ptr = &ptr_;
ptr->state = start;
// ptr->extra_data = 0; // optional
while (ptr->state != NULL)
{
ptr->state(ptr);
}
return 0;
}
void start (funcArg *ptr)
{
ptr->state = task1;
}
void stop (funcArg *ptr)
{
ptr->state = NULL;
}
void task1 (funcArg *ptr)
{
ptr->state = stop;
}
This sort of works:
#include <stdio.h>
void *a(void)
{
printf("Calling a()\n");
return NULL;
}
void *b(void)
{
printf("Calling b()\n");
return a;
}
void *c(void)
{
printf("Calling c()\n");
return b;
}
int main(void)
{
void *(*funcPtr)(void) = &c;
while (funcPtr) {
funcPtr = funcPtr();
}
}
I don't really see good uses, especially in passing the pointer to the function itself as an argument (which I why I omitted it) but whatever floats your boat. You can of course replace the arguments to whatever you need.
You could add a typedef to help out a bit with a type:
typedef void *(*myfunc)(void);
Then you could do the following:
myfunc funcPtr = &c;
// instead of: void *(*funcPtr)(void) = &c;
I don't think any of this is particularly elegant, but it should work.
Note that it doesn't matter if you assign c or &c to myfunc, or whether you return a or &a from one of the functions.

C function (without parameters) call with parameters

I'm wondering something like this is possible:
// declaration
void func();
int main()
{
int ar[] = { 1, 2, 3 };
func(ar); // call with parameter
return 1;
}
void func() // no parameters
{
// do something
}
Can someone explain me this and especially how can I access ar in func()?
In C (not C++), a function declared as func() is treated as having an unspecified number of untyped parameters. A function with no parameters should be explicitly declared as func(void).
A hack would be to exploit the GCC calling convention.
For x86, parameters are pushed into stack. Local variables are also in the stack.
So
void func()
{
int local_var;
int *ar;
uintptr_t *ptr = &local_var;
ptr += sizeof(int *);
ar = (int *)ptr;
May give you the array address in ar in x86.
For x86_64, the first parameter is stored in rdi register.
void func()
{
uintptr_t *ptr;
int *ar;
asm (
"movq %%rdi, %0"
:"=r"(*ptr)
:
:"rdi");
ar = (int *)ptr;
May give you the array address in ar in x86_64.
I have not tested these code myself and you may be to fine tune the offsets yourself.
But I am just showing one possible hack.
If you want to use any function with no parameters with any return type, it should be declared as (In C)
return_type func(void). It is only generic way of function declaration.
But any how, for your question , it possible to access but not generic..Try this program...
#include<stdio.h>
int *p;
void func();
int main()
{
int ar[] = { 1, 2, 3 };
p=ar;
printf("In main %d\n",ar[0]);
func(ar); // call with parameter
printf("In main %d\n",ar[0]);
return 1;
}
void func() // no parameters
{
printf("In func %d \n",*p);
*p=20;
}
Even this program works fine, it is not generic way and also is undefined.
if you declare function like void func (void) ,it will not work.
You can't access ar in func(), since you dont have a reference to it in func().
It would be possible if ar would be a global var or you have a pointer on it.
So that you can do something with func(), you need to pass it the input data you'll work with.
First you must declare the function properly :
// declaration
void func(int []);
The define it :
void func( int a[] )
{
// do something
printf ("a[0] = %d\n", a[0]);
}
Full code :
#include <stdio.h>
// declaration
void func(int []);
int main()
{
int ar[] = { 1, 2, 3 };
func(ar); // call with parameter
return 1;
}
void func( int a[] )
{
// do something
printf ("a[0] = %d\n", a[0]);
}
This will display :
a[0] = 1
You can implement something like this.
void func(int *p, int n);
int main()
{
int ar[] = { 1, 2, 3 };
func(ar, sizeof (ar)/sizeof(ar[0]) ); // call with parameter
return 1;
}
void func(int *p, int n) // added 2 parameters
{
int i=0;
for (i=0; i<n; ++i){
printf ("%d ", p[i]);
}
}

Compile time Error in C code [Returning an array pointer from function]

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<conio.h>
int main()
{
int i, *ptr;
ptr = func();
for(i=0;i<20;i++)
{
printf("%d", ptr[i]);
}
return 0;
}
int * func()
{
int *pointer;
pointer = (int*)malloc(sizeof(int)*20);
int i;
for(i=0;i<20;i++)
{
pointer[i] = i+1;
}
return pointer;
}
ERROR:
Conflicting type of func.
Warning:
Assignment makes Pointer from integer without a cast [enabled by default]
Why am I getting this error?
Because you're calling func() without first declaring it. This causes the compiler to assume it's going to return int, but then you store that integer in a pointer which is of course rather suspicious.
Fix by moving func() to above main(), so the definition is seen before the call, or introduce a prototype before main():
int * func();
Also, functions taking no arguments should be (void) in C, and please don't cast the return value of malloc() in C.

How to create a typedef for function pointers

I think it would be easier to use function pointers if I created a typedef for a function pointer, but I seem to be getting myself tripped up on some syntax or usage or something about typedef for function pointers, and I could use some help.
I've got
int foo(int i){ return i + 1;}
typedef <???> g;
int hvar;
hvar = g(3)
That's basically what I'm trying to accomplish I'm a rather new C programmer and this is throwing me too much. What replaces <???> ?
Your question isn't clear, but I think you might want something like this:
int foo(int i){ return i + 1;}
typedef int (*g)(int); // Declare typedef
g func = &foo; // Define function-pointer variable, and initialise
int hvar = func(3); // Call function through pointer
You are right. The function pointer can be conveniently used to point to the different functions of the same return type and taking same number of arguments.
The argument types should match the declaration of the function pointer arguments.
In your case you could define your function pointer g as:
typedef int (*g)(int); // typedef of the function pointer.
g is a function pointer for the function returning int value and taking one int argument.
The usage of function pointer could be illustrated by a simple program below:
#include<stdio.h>
typedef int (*pointer_to_function)(int first_parameter_of_type_int, int second_parameter_of_type_int);
int my_function_returning_int_and_taking_two_int_arguments(int par1, int par2)
{
int result = par1 + par2;
return result;
}
int my_mul_function(int par1, int par2)
{
int result = par1 * par2;
return result;
}
int main()
{
int res; // returning result will be here
pointer_to_function my_fun_pointer; // declare function pointer variable;
my_fun_pointer = my_function_returning_int_and_taking_two_int_arguments; // function pointer points to `my_function_returning_int_and_taking_two_int_arguments` function
res = my_fun_pointer(2,3); // Call function through pointer
printf(" result of `my_function_returning_int_and_taking_two_int_arguments` = %d \n", res);
my_fun_pointer = my_mul_function; // now function pointer points to another function: `my_mul_function`
res = my_fun_pointer(2,3); // Call function through pointer
printf(" result of `my_mul_function` = %d \n", res);
return 0;
}
OUTPUT:
result of `my_function_returning_int_and_taking_two_int_arguments` = 5
result of `my_mul_function` = 6
The original way of writing the function returning function pointer is
int (* call(void) ) (int,int);
Here call is a function which takes nothing but returns a function pointer which takes 2 arguments and returns an integer value. Pay attention to the brackets, they are absolutely necessary.
Here is the code:
#include<stdio.h>
int sum(int a,int b) //sum is the function returned by call
{
return a+b;
}
int (*call(void) ) (int ,int);
int main() {
int (*p)(int,int); // way to declare a function pointer
p=call();
printf("%d\n",(*p)(8,3));
}
int( *call(void) )(int,int) {
return sum;
}

How do I call an arbitrary C function passed to another function?

I'm writing a unit test framework (see SO for more details). Or view the code at GitHub.
Safer Code describes a way to pass functions of arbitrary types.
But how do I call such a function without knowing its types beforehand? Assume f needs no input, so f() should work on its own.
Let's say I want to populate an array using an arbitrary generator function.
void* gen_array(fp gen, size_t size) {
int i, len = gen_int() % 100;
void* arr = GC_MALLOC(len * size);
for (i = 0; i < len; i++) {
arr[i] = gen(NULL);
}
return arr;
}
It should look something like this, but I get compiler errors:
gcc -o example example.c qc.c qc.h -lgc
In file included from example.c:1:
qc.h:21: error: expected declaration specifiers or ‘...’ before ‘size_t’
In file included from qc.c:1:
qc.h:21: error: expected declaration specifiers or ‘...’ before ‘size_t’
qc.c:23: error: conflicting types for ‘gen_array’
qc.h:21: error: previous declaration of ‘gen_array’ was here
qc.c: In function ‘gen_array’:
qc.c:29: warning: dereferencing ‘void *’ pointer
qc.c:29: error: too many arguments to function ‘gen’
qc.c:29: error: invalid use of void expression
qc.h:21: error: expected declaration specifiers or ‘...’ before ‘size_t’
make: *** [example] Error 1
After thinking about some more I realize your problem your above code would never work.
You are first calling trying to call a void function with no parameters with the parameter NULL. Next you would need your code to be more generic. I placed an example below of what I mean. Now using a global variable
#include <stdio.h>
#include <stdlib.h>
typedef void (*fp)(void);
void * GEN_ARRAY_TEMP;
int gen_int() {
return 67;
}
void* gen_array(fp gen, size_t size) {
int i, len = gen_int() % 100;
void* arr = malloc(len * size);
void* arr_end = arr + len * size;
GEN_ARRAY_TEMP = arr;
while (GEN_ARRAY_TEMP <= arr_end) {
gen();
GEN_ARRAY_TEMP+=size;
}
return arr;
}
void make_int() {
(*(int*)GEN_ARRAY_TEMP) = 9;
}
int main() {
int i;
int * gen_int_array = (int*) gen_array(make_int, sizeof(int));
for(i=0;i<67;i++) {
printf("%d\n",gen_int_array[i]);
}
}
That page suggests you make the function pointer take a void*. So in order for your code to compile, you must pass it a void pointer:
typedef void* (*fp)(void*);
doit(fp f) {
f(NULL);
}
And just make sure that the function that you're calling simply ignores the parameter.
Generally speaking, these generic function pointers are used for starting threads. The void pointer is simply a pointer to a struct that holds the actual parameters.
What would you need to do is wrap your function in a void function like so
#include <stdio.h>
typedef void (*fp)(void);
int sum(int x,int y) {return x+y;}
void doit(fp f) {
f();
}
void func() {
printf("Hello %d\n",sum(1,2));
}
int main() {
doit(func);
}
You have two problems:
First, qc.h is missing a <stdlib.h> include. This is needed for use of size_t.
Second, in gen_array, you create a void *arr, then try to dereference it as an array (arr[i]). Since the compiler doesn't know the size of your array elements, it cannot fill the array. You must treat it as a char *, offset by arr + size * i, and pass it into gen rather than taking a return (returns also need to know the structure size):
// ...
char *arr = GC_MALLOC(len * size);
for (int i = 0; i < len; i++) {
gen(arr + i * size, NULL);
}
return arr;
This will of course require changing the fp type definition.
For the case where your pointer to a function 'fp' is of type which takes no argument and returns void, in which case you should declare it as :
typedef void (*fp)();
In the above case the call should be :
(*gen)();
If your pointer to the function 'fp' is of type which takes 'void *' as argument and returns void, in which case you should declare it as :
typedef void (*fp)(void *);
In the above case the call should be :
(*gen)(NULL);
or any other pointer variable you might want to pass.
As far as your example goes try this :
typedef void * (*fp)(void *);
void* gen_array(fp gen, size_t size) {
int i, len = gen_int() % 100;
void* arr = GC_MALLOC(len * size);
for (i = 0; i < len; i++) {
arr[i] = (*gen)(NULL);
}
return arr;
}

Resources