Learn to implement data structures like Heap, Stacks, Linked List and many more! Check out our Data Structures in C course to start learning today.
Next What are near, far and huge pointers? Recommended Articles. Article Contributed By :. Easy Normal Medium Hard Expert. Writing code in comment? Please use ide. Load Comments. What's New. Following is a simple example that shows declaration and function call using function pointer.
See following post for details. Following are some interesting facts about function pointers. Typically a function pointer stores the start of executable code. Below example in point 5 shows syntax for array of pointers. For example, in below program, user is asked for a choice between 0 and 2 to do different tasks. Notice that a pointer, not an array, is defined to store the array address returned by the function.
Also notice that when a local variable is being returned from a function, we have to declare it as static in the function. As we know by definition that pointers point to an address in any memory location, they can also point to at the beginning of executable code as functions in memory. After defining the function pointer, we have to assign it to a function. For example, the next program declares an ordinary function, defines a function pointer, assigns the function pointer to the ordinary function and after that calls the function through the pointer:.
Keep in mind that the function name points to the beginning address of the executable code like an array name which points to its first element. An array of function pointers can play a switch or an if statement role for making a decision, as in the next program:. Each array element must have the same parameters and return type. Void pointers are used during function declarations. If we assume that our parameters do not change when passing to a function, we declare it as const.
0コメント