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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nagaharish
    New Member
    • Sep 2013
    • 6

    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)?
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    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.

    Comment

    • nagaharish
      New Member
      • Sep 2013
      • 6

      #3
      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

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        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

        • nagaharish
          New Member
          • Sep 2013
          • 6

          #5
          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

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            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

            • nagaharish
              New Member
              • Sep 2013
              • 6

              #7
              Code:
              #include<stdio.h>
              #include<conio.h>
              void f(){
              printf("hi");
              }
              int main(){
              atexit(f);
              return 0;
              }
              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...
              Last edited by Rabbit; Sep 3 '13, 07:47 PM. Reason: Please use code tags when posting code.

              Comment

              • weaknessforcats
                Recognized Expert Expert
                • Mar 2007
                • 9214

                #8
                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));
                Then in your program:

                Code:
                #include "MyHeader.h"
                
                void f(){
                printf("hi");
                }
                
                int main()
                {
                   Nagaharish(f);
                }
                Finally, in a separate .c file code your function:

                Code:
                int Nagaharish(void (*fp)(void))
                {
                      fp();
                      return 0;
                }
                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?

                Comment

                • nagaharish
                  New Member
                  • Sep 2013
                  • 6

                  #9
                  Yeah thank you very much......
                  ...but I want another function in stdlib which got defined already....whic h takes function as argument....
                  Last edited by nagaharish; Sep 4 '13, 05:38 AM. Reason: Nothing

                  Comment

                  • weaknessforcats
                    Recognized Expert Expert
                    • Mar 2007
                    • 9214

                    #10
                    What function in stdlib do you want?

                    Comment

                    • nagaharish
                      New Member
                      • Sep 2013
                      • 6

                      #11
                      Any function which receive only function as a argument....... ?

                      Comment

                      Working...