Is there are any function like atexit in which there is no need to declare and define function pointer we only should pass the function(just like atexit function)?
function pointer like atexit which we have no need to declare and define the function
Collapse
X
-
Tags: None
-
I'm not sure what you mean. You have to pass atexit the address of a function with void arguments that returns void.
Now, the name of a function is the address of the function so you can just pass the function name to atexit. There is never any need to define an actual function pointer variable. -
Yeah I want another inbuilt function just like atexit,in which I want to just pass the function name and there shud be need to declare function pointerComment
-
Can you give a code example of what you want to do?
It doesn't matter if a function is inbuilt or not, if it has a function pointer argument all you need to do is pass in the name of the function.Comment
-
Atexit ()...for this function there is no need to declare function pointer all I have to do is to just pass the function....... ..is there is any other function just like this I mean in the same way.........I just want to pass function name....I don't want to declare function pointer(like atexit function)Comment
-
Now I am really not understanding what you mean.
In C and C++ all functions are created equal. What works for atexit will work for other functions with the same argument.
Please post some code that shows your difficulty.Comment
-
Code:#include<stdio.h> #include<conio.h> void f(){ printf("hi"); } int main(){ atexit(f); return 0; }
in the above example atexit function going to call function (f) when program going to be exit.In this code i never declared any function pointer for atexit function in this code i just passed the function name.
in the same way i want another function for which i dont want to declare function pointer or there shouldnot be any need to declare function pointer for that function explicitly in my code i just want to pass the function name...Comment
-
When you say i never declared any function pointer for atexit function in this code i just passed the function name, you are correct. You didn't declare atexit. Nonetheless atexit was declared.
In this case, atexit is declared in stdlib.h and I suspect stdlib.h was #included in conio.h.
OK, so now I think you mean how to declare a function the same as the built in functions. The answer is to declare your function in a header file and then #include the header in your own program.
To do this first create your header file:
MyHeader.h:
Code:int Nagaharish(void (*fc)(void));
Code:#include "MyHeader.h" void f(){ printf("hi"); } int main() { Nagaharish(f); }
Code:int Nagaharish(void (*fp)(void)) { fp(); return 0; }
This is usually accomplished in your compiler tool by creating a project with the two .c files in it and then building the project.
Does this help?Comment
-
Yeah thank you very much......
...but I want another function in stdlib which got defined already....whic h takes function as argument....Comment
-
-
Comment