Pointing a function to another

function pointer c
c++ function pointer as parameter
c function pointer typedef
function pointer as argument
array of function pointers c
function pointer in c pdf
c++ pass function as argument
difference between function pointer and pointer to function

Suppose I have two functions:

void DoesNothing(){}

void OnlyCalledOnce(){
    //lines of code
}

Is it possible to call OnlyCalledOnce and it actually run DoesNothing ? I imagine something like this:

void DoesNothing(){}

void OnlyCalledOnce(){
    //lines of code
    OnlyCalledOnce = DoesNothing;
}

and after that last line, whenever I called OnlyCalledOnce it would run DoesNothing.

Is it possible?


Did you try to use delegate?

class Program
{
    private static Action Call = OnlyCalledOnce;

    public static void Main(string[] args)
    {
        Call();
        Call();
        Call();
        Console.ReadKey();
    }

    static void DoesNothing()
    {
        Console.WriteLine("DoesNothing");
    }

    static void OnlyCalledOnce()
    {
        Console.WriteLine("OnlyCalledOnce");
        Call = DoesNothing;
    }
}

How to pass function pointer as parameter in C?, Is it possible to have a function as a parameter in another function? Function pointers work just like normal pointers. They point to whatever they point to. It doesn't matter whether you change the variables of any other pointers. funcPntr2 will just keep pointing to that same function.


You can simply return early in OnlyCalledOnce like this: (assuming your DoesNothing example literally does nothing - it isn't needed)

bool initialized = false;

void OnlyCalledOnce()
{
    if (initialized) return;

    // firsttimecode
    initialized = true;
}

The initialized variable will be true after first run.

The correct way to declare and assign a function pointer is done by , What is the correct way to declare and assign a function pointer? Example Uses of Function Pointers Functions as Arguments to Other Functions If you were to write a sort routine, you might want to allow the function's caller to choose the order in which the data is sorted; some programmers might need to sort the data in ascending order, others might prefer descending order while still others may want something similar to but not quite like one of those choices.


Another way you could solve this is to maintain a list of strings that represent the methods that have been called. The strings don't even have to be the method name, they just need to be unique to each method.

Then you can have a helper method called ShouldIRun that takes in the function's unique string and checks to see if it exists in the list. If it does, then the method returns false, and if it doesn't, then the method adds the string to the list and returns true.

The nice thing here is that you don't have to maintain a bunch of state variables, you can use this with as many methods as you want, and the methods themselves don't need any complicated logic - they just ask the helper if they should run or not!

public class Program
{
    private static List<string> CalledMethods = new List<string>();

    static bool ShouldIRun(string methodName)
    {
        if (CalledMethods.Contains(methodName)) return false;
        CalledMethods.Add(methodName);
        return true;
    }

    // Now this method can use method above to return early (do nothing) if it's already ran
    static void OnlyCalledOnce()
    {
        if (!ShouldIRun("OnlyCalledOnce")) return;

        Console.WriteLine("You should only see this once.");
    }

    // Let's test it out
    private static void Main()
    {
        OnlyCalledOnce();
        OnlyCalledOnce();
        OnlyCalledOnce();

        GetKeyFromUser("\nDone! Press any key to exit...");
    }
}

Output

Function pointer, Is the type of “pointer-to-member-function” different from “pointer-to-function”? void* pointers are pointers to data, and function pointers point to functions. C programming allows passing a pointer to a function. To do so, simply declare the function parameter as a pointer type. Following is a simple example where we pass an unsigned long pointer to a function and change the value inside the function which reflects back in the calling function −


As already stated, you can use this:

private bool isExecuted = false;

void DoesNothing(){}

void OnlyCalledOnce(){
    if (!isExecuted)
    {   
        isExecuted = true;
        //lines of code
        DoesNothing();
    }
}

If you have multiple threads etc, you can do a lock(object) ..

Pointers to Member Functions, C++ FAQ, This point in particular is very useful in C. In C, we can use function pointers to to sort arrays in ascending order or descending or by any other order in case of  In general practice, and with the *intended-way-to-write-C*, you pass a pointer. However, if you are open to hackery, you can create a byte array. With a function pointer you can copy the bytes of the function into the array or just plain add bytes of your own. These bytes, are of course, assembled instructions.


What's your problem with this?

void DoesNothing()
{
}

void OnlyCalledOnce()
{
    DoesNothing();
}

It will run DoesNothing() once you run OnlyCalledOnce()

Function Pointer in C, Passing functions as arguments to other functions​​ One of the most useful things to do with function pointers is pass a function as an argument to another function. Functions used as arguments to another function are sometimes called callback functions. Write a simpler function that agrees with the given function at all but one point? (x^3-x)/(x-1) (each function above is from a separate question) Answer Save. 2


7.8, Function pointers in C. Let's start with a basic function which we will be pointing to: Passing the pointer to another function is basically the same: int add2to3(int​  A pointer to an array contains the address of the array's first element. Simple as that.


How do function pointers in C work?, We will use this function pointer to pass functions as arguments to another function. Declare a function pointer with function prototype it can point. A pointer is a variable that stores a memory address. Pointers are used to store the addresses of other variables or memory items. Pointers are very useful for another type of parameter passing, usually referred to as Pass By Address. Pointers are essential for dynamic memory allocation.


6.6. Functions can Call Other Functions, To draw a rectangle we need to be able to call a function with different arguments for The point is that the program doesn't “understand” that you're drawing a