Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

C problem callback function

Status
Not open for further replies.

Sadegh.j

Advanced Member level 3
Joined
Nov 29, 2006
Messages
749
Helped
48
Reputation
98
Reaction score
15
Trophy points
1,298
Activity points
5,585
Hi

I am not a programmer, electronic engineer in fact. But I am programming a chip and need callback functions.

Apparently, in a C code, a callback function is used which calls another function in another C code, without including it as a header file. is it possible?

Thanks
 

Yes, because callback function is passed as "pointer to function" to the caller funciton which will call the callback function. So while writing the caller function, we don't need header file for callback function. Though in caller function, you have to take care of calling callback function with right number/type of arguments.
 

very interesting, could you please explain more? or give me a reference to read from.

Thanks
 

Callback function needs a function prototype as any regular function call. As a basic difference, there is no static function address rather than a function pointer. Fucntion pointers are widely used in programming, e. g. with C++ classes.

The particulary thing will callback functions, to my opinion, is the opposite call direction implied here. You e. g. call a windows enumeration function and provide a callback function within your code segment, that is called back from operation system to deliver the data. The mechanism implies, that the function prototype is clearly defined, normally it is included in a header file shipped with the compiler.
 

FvM said:
Callback function needs a function prototype as any regular function call. As a basic difference, there is no static function address rather than a function pointer. Fucntion pointers are widely used in programming, e. g. with C++ classes.

The particulary thing will callback functions, to my opinion, is the opposite call direction implied here. You e. g. call a windows enumeration function and provide a callback function within your code segment, that is called back from operation system to deliver the data. The mechanism implies, that the function prototype is clearly defined, normally it is included in a header file shipped with the compiler.

Here we are providing callback functino to "windows enumeration function", but when "windows enumeration function" was compiled, did it have/need header file of your callback function ?? It can't be right ?
 

The point is, that my callback function must use the windows defined prototype. This isn't a link mechanism but a type definition.
 

There's nothing intrinsically special about a "callback" function in C. It's simply a function that's called from another function by passing it a pointer.

When calling any C function, you need to pass the parameters correctly. To do that, you can either put the called function's source code above the calling function in the same source file, or you can use a function prototype. Most programmers use the second method, and then put all their function prototypes into a header file so the user can include them conveniently. The header file may also contain additional useful things such as macro definitions related to the function.

Callback functions are sometimes used with interrupt handlers and device drivers, and so may have special requirements such as reentrancy and unusual parameter passing order, so read the documentation carefully.
 

Guys, you got a bit technical and I got lost some where in the middle of your discussions.

Let me explain more, there is a project which includes many files. they all get complied, linked and the project is then built using IAR.

The funny thing is that the file in which the application is written is kind of isolated. It is not included in ANY other file and the only file that it includes is its header. The void main void {} also does not see this. but the code is somehow working. The only way to explain this is if call back functions do not need the #include line to be called. The application is filled with call back functions. :-(

Is it possible?
 

A lot is possible using callback functions. The interesting questions for the code using callback are: how does it get the correct function address and how does it know the correct parameter type and order respectively where does it get a function prototype from?
 

and do you possibly have the answer.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top