Pointer In Function Name / Pointer Function - c

I'm learning C, I was looking at https://github.com/mruby/mruby/blob/master/src/load.c and this line made me very confused:
mrb_irep* read_irep_record_1
On line 40.
I can see that this is a pointer of some sort.
What I'd like to know is the following
What does this do?
How do you use them?
What are these called?
How do they work?
How can I replicate this in a program?
I've only this used in C projects, is it recommended to use these in C++? Can you do this in C++?
I searched a bit on Stackoverflow for pointer functions but couldn't find anything like this.
Thanks in advance!

That line is simply declaring a function that returns a pointer to mrb_irep. For example, what does a function declared as int foo() return? Well it returns an int, as we see in the declaration. Similarly, a function declared as mrb_irep* read_irep_record_1(...) returns a variable of type mreb_irep*, or a pointer to a struct called mreb_irep.

Related

How to declare a function if it's return value and parameters are given in command line and unknown before implementation? [duplicate]

I am presently in a case where I need to call a lot of function pointers that has been extracted at runtime. The problem is that the arguments are unknown at compilation time.
But, at runtime I receive datas that allows me to know the arguments of the function and I can even store the arguments in a char* array. The problem is that I don't have a function pointer model to cast it into.
In high level language, I know there is function like "InvokeMethode(String name,Byte[] args)" that interpret the bytes array like arguments. Since reflection does not exist in C, I have no hope to see this with a function pointer.
One solution that I have in mind (and it's really bad), is to create a model of function pointer at compilation time that will cast in a "hardcoded way" the ptr to the right type to use like this:
void callFunc64Bits(void* funcPtr,long long args);
void callFuncVoid(void* funcPtr);
The problem is that I will have to create like 100 function like this that will cast the pointer correctly.
Is there a way to do it more efficiently?
Thank you very much!
This is a hard problem without, unfortunately, good or easy answers.
See this former SO question: Run-time parameters in gcc (inverse va_args/varargs)
See this C FAQ question: http://c-faq.com/varargs/invvarargs.html
See this collection of "wacky ideas" by the C FAQ list maintainer: http://c-faq.com/varargs/wacky.html
Addendum: see this former SO question: How to call functions by their pointers passing multiple arguments in C?
...which mentions "libffi": http://sourceware.org/libffi/

C late binding with unknown arguments

I am presently in a case where I need to call a lot of function pointers that has been extracted at runtime. The problem is that the arguments are unknown at compilation time.
But, at runtime I receive datas that allows me to know the arguments of the function and I can even store the arguments in a char* array. The problem is that I don't have a function pointer model to cast it into.
In high level language, I know there is function like "InvokeMethode(String name,Byte[] args)" that interpret the bytes array like arguments. Since reflection does not exist in C, I have no hope to see this with a function pointer.
One solution that I have in mind (and it's really bad), is to create a model of function pointer at compilation time that will cast in a "hardcoded way" the ptr to the right type to use like this:
void callFunc64Bits(void* funcPtr,long long args);
void callFuncVoid(void* funcPtr);
The problem is that I will have to create like 100 function like this that will cast the pointer correctly.
Is there a way to do it more efficiently?
Thank you very much!
This is a hard problem without, unfortunately, good or easy answers.
See this former SO question: Run-time parameters in gcc (inverse va_args/varargs)
See this C FAQ question: http://c-faq.com/varargs/invvarargs.html
See this collection of "wacky ideas" by the C FAQ list maintainer: http://c-faq.com/varargs/wacky.html
Addendum: see this former SO question: How to call functions by their pointers passing multiple arguments in C?
...which mentions "libffi": http://sourceware.org/libffi/

How can one implement an array of functions on C?

I am trying to implement an interpreter. I'd love to go with GCC first class labels to make it threaded code, but I should hold on to a standard this time, so naturally I am left with function table. So, I'm doing this:
unsigned short int FUN_TABLE[MAX_FUN] (void*);
And I want to fill it with functions, each getting pointer to its operands, doing its part, returning length of the whole instruction in memory to a dispatcher.
The thing is, I can't even compile it due to the following error: declaration of FUN_TABLE as array of functions. Considering it is exactly what I am trying to achieve, why is this an error, why should I pay it attention, and if I shouldn't, how to suppress it in elegant and standardized manner?
You can define an array of function pointers like this (pseudocode):
int (*funcArr2[10])(param, param, ...) = {NULL};
However, you should be aware that this means that all these functions have the same set of arguments. You can not declare an array with function pointers to totall different functions with regard to their signature.
GCC is telling you: "there is no such thing as an array of functions".
Considering it is exactly what I am trying to achieve, why is this an error, why should I pay it attention
Because you are trying to achieve something that does not exist in the C language. But instead, you can achieve the desired functionality through an array of function pointers.
The syntax of declaring a function pointer is
return_type (*func_ptr_name)(parameters)
and the syntax for declaring an array of function pointers is
return_type (*func_ptr_name[n])(parameters)
Since that syntax is quite obscure, you will not want to use it. The solution is to use typedefs:
typedef unsigned short (*func_table_t)(void*);
// declare an array of function pointers, using readable syntax:
func_table_t func_table [MAX_FUNC] =
{
&some_function,
&some_other_function,
...
};
Arrays of functions aren't legal. Your easiest work around would be an array of pointers to functions -- but this implies that each function being pointed to from the array has the same signature.

Pointers clarification in makecontext function

I have been implementing a user threads library as part of my assignment.
I didn't understand the makecontext function:
makecontext(&(mainthread->threadctx),(void(*)(void))start_funct,1,args)
What does (void(*)(void))start_funct exactly mean? And why do I have to write it this way?
Can't I just write it as
makecontext(&(mainthread->threadctx),start_funct,1,args) ?
Please be patient with me, I am not yet comfortable with pointers :)
void(*)(void) means "pointer to a function that takes no parameters and returns void".
Therefore (void(*)(void))start_funct is casting start_funct (which we can assume is some kind of function pointer)` to the above type. (There is a very useful online tool that can help you with this until you get more comfortable reading declarations).
You have to write it this way because the signature of start_funct is not void start_funct(void), so casting is required.

printf function in c

Does printf function in c calls argument by reference or by value?
Everything in C is passed by value.
Even things that look like they're passed by reference (i.e., pointers to variables so that you can change the underlying variables) are in fact the values of the pointers being passed by value.
There's no pass by reference in C, everything is by value or pointer-to-address by value
call by reference is not applicable in c.
As in c we can't create alias of any variable,so eiher call by value or call by pointer mechanism is supported here..

Resources