Embedded C function macro problem - c

I came across this in embedded hardware using C.
#define EnterPWDN(clkcon) ( (void (*)(int))0xc0080e0 ) (clkcon)
I have no idea how is this function macro working. I understand clkcon is the function parameter to EnterPWDN, but what is happening after that?

It casts the address 0xc0080e0 to a pointer to function taking an int and returning void, and calls that function, passing clkcon as the parameter.
Spelled out:
typedef void (func_ptr*)(int);
func_ptr func = (func_ptr)0xc0080e0;
func(clkcon);
(If you haven't come across function pointers, you might want to grab a good C introduction and read up on the subject.)

Its a void function pointer that takes an int as a parameter. The function is held at the specific memory address 0xc0080e0.
(void (*)(int))
The above is a function pointer declaration. First comes the void return type. Next comes the fact that its a pointer and finally the int tells you what the parameter to the function is. The memory address is the location the function is stored at and the whole thing is casting that memory address into the correct function pointer type and then calling the function and passing "clkcon" to it.

Excellent answers Goz and sbi, but to put it another way:
At a specific address (0xc0080e0) in memory, possibly in a ROM, there is a function. You call this function with the int clkcon argument.

Related

Defining A Function Pointer Using Assignment In c

I'm learning C. I know what the first line is doing; it's making a pointer to a function with no arguments and it returns an int. But wtf is the second doing?
My guess is that it is casting an int into a function? But what does it mean to turn an int into a function?
Also, why does it cause an error when I try to call the function: 'function()'?
int (*function) ();
function = (int (*) ()) (1000);
Overall, the code is nonsense. Where did you get it from?
it's making a pointer to a function with no arguments
Rather, it is making a pointer to a function with obsolete style parameter list.
But wtf is the second doing?
It assigns the function pointer to point at address 1000 (decimal), my means of a cast from int to the function pointer type.
why does it cause an error when I try to call the function: 'function()'?
Likely because there is no such function allocated at address 1000. You might not even have access to that area etc.
learning function pointer is pain in ass.
simple example maybe help for better understand.
suppose you want point to this function:
int sum (int a , int b) {
return a+b;
}
so you need define a pointer with same type:
int *pointerToSum (int, int);
and simply assign it to function:
pointerToSum = ∑
if you want use it:
(*pointerToSum)(3, 5); //8
you can use function-pointer tag to find out more complex answers.
your question delete by people because there is duplicate question asked before.
I implement function-pointer for myself and it's a little complex and you can see in : exercise of function pointer
I wish you the best.

how should i interpret this typedef declaration in c? [duplicate]

I am trying to understand what this means, the code I am looking at has
in .h
typedef void (*MCB)();
static MCB m_process;
in .C
MCB Modes::m_process = NULL;
And sometimes when I do
m_process();
I get segmentations fault, it's probably because the memory was freed, how can I debug when it gets freed?
It defines a pointer-to-function type. The functions return void, and the argument list is unspecified because the question is (currently, but possibly erroneously) tagged C; if it were tagged C++, then the function would take no arguments at all. To make it a function that takes no arguments (in C), you'd use:
typedef void (*MCB)(void);
This is one of the areas where there is a significant difference between C, which does not - yet - require all functions to be prototyped before being defined or used, and C++, which does.
It introduces a function pointer type, pointing to a function returning nothing (void), not taking any parameters and naming the new type MCB.
The typedef defines MCB as the type of a pointer to a function that takes no arguments, and returns void.
Note that MCB Modes::m_process = NULL; is C++, not C. Also, in C, the typedef should really be typedef void (*MCB)(void);.
I'm not sure what you mean by "the memory was freed". You have a static pointer to a function; a function cannot be freed. At most, your pointer has been reset somewhere. Just debug with a memory watch on m_process.
Let's take an example
typedef void (*pt2fn)(int);
Here, we are defining a type pt2fn. Variables of this type point to functions, that take an integer as argument and does not return any value.
pt2fn kk;
Here, kk is a variable of type pt2fn, which can point to any function that takes in an integer as input and does not return any value.
Reference:https://cs.nyu.edu/courses/spring12/CSCI-GA.3033-014/Assignment1/function_pointers.html
It's a function pointer. You get a SEGMENTATION FAULT because you are trying to make a call to a function which address is invalid (NULL).
According to your specific sample, the function should return no value (void) and should receive no parameters ().
This should work:
void a()
{
printf("Hello!");
}
int main(int arcg, char** argv)
{
m_process = a;
m_process(); /* indirect call to "a" function, */
// Hello!
}
Function pointers are commonly used for some form of event handling in C. It's not its only use though...

C - function returning pointer to function

What is the proper C syntax for a function returning pointer to another function (which again may return a pointer to some thrid function etc)? I know that we can define a function as a local variable inside another function (but we need to know the address or it is useless):
int*(*a)(int) = (void*)0;
This is local variable a which represents a function which has int as a parameter and returns pointer to int, while the address of the function is 0. How can I have a function which returns, instead of pointer to int, a pointer to a function requiring char as a parameter and returning a pointer to int? This is what I've tried:
int*(*)(char)(*a)(int) = (void*)0;
But, it is a syntax error. Is there a way to do it, or maybe, the only way is to return void* and then to cast it again to function?
Edit
I am not looking only for a solution which works. I know it can be done using typedef or just by returning a generic pointer and then casting to another function. But, I am writting a code highlighter for C and I want to cover all cases which are defined by ISO C, so I am wondering does ISO C allow double returning functions, if yes, what is the proper syntax, if no, can it be found somewhere in documentation?
You could use typedefs to simplify this (and make sure you can actually understand the code a week later):
typedef int*(*FuncA)(int);
typedef FuncA(*FuncB)();
FuncB a = (void*)0;
Obviously use more descriptive names than I have done here.

Set a function pointer to a static address

I'm injecting a DLL into another process and want to call a function that is in that binary based on it's address (0x54315).
How can I actually declare a function, and then set it to this address?
#define FUNC 0x54315
void *myFuncPtr;
int main()
{
myFuncPtr = FUNC; // pretty sure this isn't how
myFuncPtr(); // call it?
}
The existing answers work, but you don't even need a variable for the function pointer. You can just do:
#define myfunc ((void (*)(void))0x54315)
and then call it as myfunc() just like you would an ordinary function. Note that you should change the type in the cast to match the actual argument and return types of the function.
You need to define myFuncPtr as a function pointer, a void* isn't callable.
Best to use a typedef for that:
typedef void (*funptr)(void);
funprt myFuncPtr;
(Assuming your function takes nothing and returns nothing.)
Then you'll get a warning on the assignment - use a type cast to "silence" it, since this is indeed what you need to do.
You're pretty much on your own with this though, if the signature doesn't match, the calling convention is wrong, or the address is wrong, the compiler cannot validate anything and you get to pick up the pieces.
Your code should work once the syntax is corrected to actually be a function pointer. I failed to read it properly for my first version of this answer. Sorry.
As stated by Mat, the proper syntax for a function pointer would be:
void (*myFuncPtr)(void) = (void (*)(void)) FUNC;
This is often simplified by using a typedef since the C function pointer syntax is somewhat convoluted.
Also, you're must be really sure the function to be called is at that same exact address every time your injected DLL runs. I'm not sure how you can be sure of that, though ...
Also, you would need to pay attention to the calling conventions and any arguments the function at FUNC might be expecting, since if you get that wrong you will likely end up with stack corruption.

Pointer to function in ROM

I have microcontroler that I am working with. When debugging it is necessary to call a function from that is hard coded in ROM. Technical Reference shows how to do this:
# define Device_cal (void(*)(void))0x3D7C80
and calling procedure looks like this:
(*Device_cal)()
I can't understand what actually happens here, so my question is:
How does it work?
void (*) (void) is a type. It's a pointer to a function that takes no parameter and returns void.
(void(*)(void)) 0x3D7C80 casts the 0x3D7C80 integer to this function pointer.
(*Device_cal)() calls the function.
(Device_cal)() would do the exactly the same.
The parentheses around *Device_cal and Device_cal are required because otherwise the cast to the integer would not have the higher precedence.
The #define causes (*Device_cal)() to be expanded into this immediately before compiling:
(*(void(*)(void))0x3D7C80)()
The void(*)(void) is a declaration for a function pointer that takes void and returns void types. The (*()) represents a cast for the next token in the expression (0x3D7C80). Thus this asks to treat the data at location 0x3D7C80 as a function. The final () calls the function with no arguments.
well, you "define" a pointer to function, and call it.
void(*)(void) mean a pointer to function, that gets no arguments, and return void.
If you cast 0x3D7C80 to that type, and call it, you basically call the function that its address is 0x3D7C80.
This is not an answer (that has already been done satisfactorily), but some advice:
I would suggest the following method instead:
typedef void (*tVOID_ROMFUNCTION_VOID)( void ) ;
tVOID_ROMFUNCTION_VOID Device_cal = (tVOID_ROMFUNCTION_VOID)0x3D7C80 ;
Device_cal() ;
That way you can create any number of global function pointers on initialisation while the calls look like normal statically linked functions. And you avoid confusing pre-processor macros voodoo at the same time.
By creating different function-pointer types with different signatures, the compiler will be able to perform some parameter type checking for you too.
The symbol is pasted in which creates a temporary (un named ) pointer to a function at a fixed memory location and then calls it via dereferencing.

Resources