473,399 Members | 3,401 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

function pointer like atexit which we have no need to declare and define the function

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)?
Sep 1 '13 #1

✓ answered by weaknessforcats

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:

Expand|Select|Wrap|Line Numbers
  1. int Nagaharish(void (*fc)(void));
  2.  
Then in your program:

Expand|Select|Wrap|Line Numbers
  1. #include "MyHeader.h"
  2.  
  3. void f(){
  4. printf("hi");
  5. }
  6.  
  7. int main()
  8. {
  9.    Nagaharish(f);
  10. }
Finally, in a separate .c file code your function:

Expand|Select|Wrap|Line Numbers
  1. int Nagaharish(void (*fp)(void))
  2. {
  3.       fp();
  4.       return 0;
  5. }
  6.  
You now compile both the program .c file and the file with your function. The two files are linked together after each is compiled. The linked file is your .exe.

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?

10 1656
weaknessforcats
9,208 Expert Mod 8TB
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.
Sep 1 '13 #2
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 pointer
Sep 2 '13 #3
weaknessforcats
9,208 Expert Mod 8TB
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.
Sep 2 '13 #4
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)
Sep 3 '13 #5
weaknessforcats
9,208 Expert Mod 8TB
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.
Sep 3 '13 #6
Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. #include<conio.h>
  3. void f(){
  4. printf("hi");
  5. }
  6. int main(){
  7. atexit(f);
  8. return 0;
  9. }
This program compiles succcessfully and prints "hi" when program going to be exit
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...
Sep 3 '13 #7
weaknessforcats
9,208 Expert Mod 8TB
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:

Expand|Select|Wrap|Line Numbers
  1. int Nagaharish(void (*fc)(void));
  2.  
Then in your program:

Expand|Select|Wrap|Line Numbers
  1. #include "MyHeader.h"
  2.  
  3. void f(){
  4. printf("hi");
  5. }
  6.  
  7. int main()
  8. {
  9.    Nagaharish(f);
  10. }
Finally, in a separate .c file code your function:

Expand|Select|Wrap|Line Numbers
  1. int Nagaharish(void (*fp)(void))
  2. {
  3.       fp();
  4.       return 0;
  5. }
  6.  
You now compile both the program .c file and the file with your function. The two files are linked together after each is compiled. The linked file is your .exe.

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?
Sep 3 '13 #8
Yeah thank you very much......
...but I want another function in stdlib which got defined already....which takes function as argument....
Sep 4 '13 #9
weaknessforcats
9,208 Expert Mod 8TB
What function in stdlib do you want?
Sep 4 '13 #10
Any function which receive only function as a argument.......?
Sep 4 '13 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Skavenger | last post by:
Hi, I'm attempting to use a class member function pointer to call a relevant function. This is done like this.... typedef void(SampleA::*SAMPLEAFUNC)(void); class SampleA { public:...
3
by: Dennis Chang | last post by:
Hi all, I was reading about function pointers and came across something which intrigued me. K&R2 calls qsort (pg.119) within main as so: qsort( (void **) lineptr, 0, nlines-1, (int (*) (void...
17
by: Charles Sullivan | last post by:
The library function 'qsort' is declared thus: void qsort(void *base, size_t nmemb, size_t size, int(*compar)(const void *, const void *)); If in my code I write: int cmp_fcn(...); int...
2
by: David Rose | last post by:
I have a DLL (not .NET) that takes a function pointer argument and calls that function with an integer argument. The DLL is being called from C#. So far, it is partially working, but the...
1
by: Assertor | last post by:
Hi All, Except function pointer to a static member funciton of a class (or a function). As the following code, I could pointer a non-static member function of a class. And "Equal" was...
4
by: ranjmis | last post by:
Hi all, I have come across a piece of code wherein a function returns a function pointer as it seems to me but not very clear from the prototype. As shown below - although return type is void...
2
by: hs.samix | last post by:
Hello, I am trying to use a library which has a function, lets call it an external function, which wants a function pointer as one of its arguments. If I use that library in a C program, all...
1
by: manu1001 | last post by:
I've an old C function that calls a function pointed by a global function pointer. How do I get it to call a member function of a class object determined at run-time. It'll be complied in a C++...
4
by: prashant.khade1623 | last post by:
Hi all, Are function pointer and pointer to a function same ? How do we declare a function pointer ? How to declare an array of function pointers ? Can you please give some examples for...
4
by: Immortal_Nephi | last post by:
I had a lot of research to see how function pointer works. Sometimes, programmers choose switch keyword and function in each case block can be called. Sometimes, they choose ordinary function...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.