pointer to function pointer - expression must be a modifiable lvalue - c

I want to acquire address of a needed function in function Check_Commands, put it in pointer fptr, and then call it. But, when trying to compile this code, I get following message:
"Error[Pe137]: expression must be a modifiable lvalue"
am I missing something?
void main(void)
{
...
void(*fptr)(CmdDataType);
Check_Commands(&fptr);
(*fptr)(&CmdData);
}
void Check_Commands(void (**ptrfuncptr)(CmdDataType))
{
...
**ptrfuncptr=&DispFirmware;
...
}
void DispFirmware(CmdDataType *CmdData_ptr)
{
...
}

This:
**ptrfuncptr=&DispFirmware;
should just be
*ptrfuncptr = DispFirmware;
Also there's no need to dereference a function pointer when calling, the name of a function can be thought of as a pointer to it so an ordinary call works just like that through a pointer.

There were a couple issues with your code. Here's the fixed version:
void main(void)
{
CmdDataType CmdData;
void (*fptr)(CmdDataType *);
Check_Commands(&fptr);
(*fptr)(&CmdData);
}
void Check_Commands(void (**ptrfuncptr)(CmdDataType *))
{
*ptrfuncptr=&DispFirmware;
}
void DispFirmware(CmdDataType *CmdData_ptr) { }
fptr is a pointer to a function which takes a CmdDataType pointer as a parameter, so that needed to be fixed.
And in the function Check_Commands the function pointer needs to be dereferenced only once.

Related

Invoking function from the 2d array of function pointers

#include <stdio.h>
void test1(){ printf("test1\n"); }
void test2(){ printf("test2\n"); }
void test3(){ printf("test3\n"); }
void test4(){ printf("test4\n"); }
void (*hv_ptr[2])() = {test1,test2};
void (*dev_ptr[2])() = {test3,test4};
void (**ptr[2])() = {hv_ptr, dev_ptr};
int main() {
ptr[0][0];
ptr[0][1];
ptr[1][0];
ptr[1][1];
return 0;
}
I have declared the array of pointers to function pointers. But with this code, functions are not getting called. Kindly let me know what is going wrong here.
ptr is an array of pointers to pointers to functions, so ptr[0] is a pointer to a pointer to a function, so ptr[0][0] is a pointer to a function.
Thus, in the expression statement ptr[0][0];, the expression is just a pointer to a function. It does not contain a function call.
ptr[0][0](); is a call to the function pointed to by ptr[0][0].

double pointers error in C when compiling error when using void as well

I'm new to C and im currently learning about pointers.
I'm not sure why I am getting an error with the following sections of code in regards to pointers :
char ch;
char** pointer;
pointer = &ch;
and
int function1(void)
{
return 42.0;
}
void function2(void)
{
void (*pointer)(int);
pointer = &function1;
...
}
Any help will be appreciated :)
The very first problem is that you are using a double pointer in char** pointer ,as you are not storing the address of some other pointer so you should use char *pointer instead.
Then your function1 has return type as int but you are returning a float value ,although it won't give you any error but it can create some logical issues in your program,so better to properly write the return type in function definition and its prototype.
Then the next problem is in the function2,your function1 returns int but does not take any arguments but your function pointer return void and take int ,so you should better modify this to
int (*pointer)(void);
and then store the address of function1 in pointer ,it will work fine.
* is a single pointer and ** is a pointer to pointer.
So instead of
char** pointer;
It should be:
char* pointer;
In the second case, the function pointer prototype is not matching the prototype of the function it is pointing to.
So instead of
void (*pointer)(int);
it should be:
int (*pointer)(void);
you second section have some mistakes
you function1() return int and not take args
but your fucntion ptr return void and take int
so change it to:
int (*pointer)(void);
pointer = &function1;

Function pointer as a const argument

Is it possible to pass a function pointer as a const argument?
I get the following gcc error:
warning: type defaults to 'int' in declaration of 'f'
[-Wimplicit-int] void execute(void (const *f)(void));
...when compiling this code:
#include <stdio.h>
void print(void);
void execute(void (const *f)(void));
int main(void)
{
execute(print); // sends address of print
return 0;
}
void print(void)
{
printf("const!");
}
void execute(void (const *f)(void)) // receive address of print
{
f();
}
This is not a duplicate of What is meaning of a pointer to a constant function?, which addresses pointers to const functions as opposed to a const function argument.
The syntax you want is:
void execute(void (* const f)(void));
This says that f is a const pointer to a function that takes no arguments ((void) is a special syntax for no arguments) and returns nothing (void). A const on the right side of an * says that the pointer is const. A const on the left side says the pointer points to something that is const.
The fact that it is a const pointer means that you will not change f (assign a new value) in the function, and the compiler should give you an error if you do (unless the change is hidden from it, which it can be by casts). It does not say anything about the function itself—the function pointed to is not const in any sense because the parameter is const.
When you wrote:
void execute(void (const * f)(void));
the const on the left side of the * means that the pointer was pointing to something that was const, rather than that the pointer itself was const. Since it was saying the pointer was pointing to something there and you did not list a specific type, the compiler warned you that the type was missing (and defaulted to int).
The const keyword should be placed between the pointer symbol (*) and the argument name (here, f):
void execute(void (* const f)(void))
{
f();
}
Now if you try to change f's value:
void execute(void (* const f)(void))
{
f = print;
f();
}
...your compiler should output an error similar to this one, as expected:
Error: cannot assign to variable 'f' with const-qualified type

unusual function pointer parameter syntax

is there any difference between this two syntaxes?
void fun( void (*funptr)() )
{
funptr(); // calls the function
}
void fun( void funptr() )
{
funptr(); // calls the function
}
i'd always been using the first form, but i've just seen the second one and it seems it behaves exactly the same, while the syntax is clearer.
There is no difference. In both cases, funptr has type void (*)(), a function pointer type.
C99 standard, section 6.7.5.3, paragraph 8:
A declaration of a parameter as ‘‘function returning type’’ shall be
adjusted to ‘‘pointer to function returning type’’, as in 6.3.2.1.
There is no difference, fundamentally, in either form. For both you have a function that takes a function pointer as its argument. The function pointer must point to a function with this prototype: void foo(void);.
Below is an example of use:
void fun1(void (*funptr)(void))
{
funptr(); // calls the function
}
void fun2(void funptr(void))
{
funptr(); // calls the function
}
void foo(void)
{
}
int main(void)
{
void (*pFun)(void) = foo;
fun1(pFun);
fun2(pFun);
return 0;
}

Function pointer doubt

Please tell me what will the call to given function return and how? The code:
typedef struct {
int size;
ptrdiff_t index;
void (*inlet) ();
int argsize;
ptrdiff_t argindex;
} CilkProcInfo;
/*
* Returns a pointer to the slow version for a procedure
* whose signature is p.
*/
/* the function definition is - */
static void (*get_proc_slow(CilkProcInfo *p)) () {
return p[0].inlet;
}
/*The function gets called as -*/
(get_proc_slow(f->sig)) (ws, f);
/*where f->sig is a pointer to CilkProcInfo struct*/
In your CilkProcInfo structure, inlet is a pointer to a function that takes an unspecified number of arguments and does not return a value, like void foo();.
In the line
(get_proc_slow(f->sig)) (ws, f);
the get_proc_slow(f->sig) call returns this function pointer, so it is equivalent to
(f->sig[0].inlet) (ws, f);
So if your f->sig[0].inlet points to the function foo(), it is equivalent to the call
foo (ws, f);
I should admit that the static void (*get_proc_slow(CilkProcInfo *p)) () {... syntax is a bit unfamiliar to me.
get_proc_slow() returns a function pointer of type void(*)() which the code then calls. So when you do:
(get_proc_slow(f->sig)) (ws, f);
It's basically same as doing:
void (*fptr)() = get_proc_slow(f->sig);
fptr(ws, f);
It looks like it's a function that returns a pointer to a function whose return value is void that has no parameters (void(*)()) and that accepts a pointer to a CilkProcInfo struct as a parameter. I'm not sure why you'd need the p[0].inlet construct though. Couldn't you just return it as p->inlet?
Oh yeah, and get_proc_slow is the name of the function that returns said function pointer.
static void (*get_proc_slow(CilkProcInfo *p)) () {
return p[0].inlet;
}
Reading from the name out, taking care with the grammar rules: get_proc_slow is a function (with internal linkage) that takes a pointer to a CilkProcInfo struct and returns a pointer to a function taking unspecified arguments and returning no value (void).
(get_proc_slow(f->sig)) (ws, f);
This statement calls the get_proc_slow with an appropriate parameter (f->sig is a pointer to a CilkProcInfo) and then uses the return value (a pointer to a function) to call that function with ws and f as arguments.

Resources