YTread Logo
YTread Logo

Function Pointers in C / C++

May 04, 2020
In this lesson we are going to talk about

function

pointers

. Function

pointers

, as the name suggests, are used to store

function

addresses. Until now, we have mainly used pointers as variables that would store addresses of other variables. Basically, pointers are data types that can be used to store the address of some data stored in the computer's memory or in other words to point or reference some data and it does not always have to be a variable, the Data can also be stored as constants. and we use pointers not only to store the address, we can dereference and get the value at any address the pointer points to.
function pointers in c c
Now we are saying that we can have pointers that can store function addresses or in other words can point to functions and You can use a pointer to the function to dereference and execute the function and this is really interesting. Some basic questions will appear. What would actually be the address of a function? Even if we can have pointers to functions, what are the use cases where we might want to have them? There are really interesting use cases for function pointers. We will talk about them later. Let's first try to understand the core logic here. Once again, I've drawn this familiar diagram: The memory allocated to an application or program can typically be divided into these four segments: We've talked quite a bit about this in our previous session.
function pointers in c c

More Interesting Facts About,

function pointers in c c...

Okay, now a program is basically a set. or sequence of instructions you would give the computer to perform a task. We can write our program in a high-level language like C or C plus plus, but at the lowest level of its architecture, the computer understands and executes only binary. Any instructions that must be executed by the computer must be encoded in binary. Of course, there will be some rules for coding, so what we do is write our program or set of instructions in a high-level language like C or C plus plus and pass it to a program called compilers as input and corresponding to the source .
function pointers in c c
The code compiler generates what we call machine code or executable code, which are instructions encoded in binary, something similar to what I'm trying to show here. The compiler basically takes one or more source files, let's say the program that has been written here is in a file called program dot. C. Now, a compiler for C language will take this file as input and create an executable file that will have the machine code. On a Windows machine, executable files have an exe extension. An executable file will be stored on a disk drive or some secondary storage device as long as we say memory, just memory in the context of programming we mean that random access memory or RAM, which we also call main memory, is primary storage .
function pointers in c c
So whatever application memory we're talking about here will be a part of main memory. A program or application gets a chunk of memory only when it starts execution, when the application finishes execution this memory is reclaimed. What actually happens is that when we run a program when a program started running, it is allocated an amount of memory. And that's what we call application memory here. The code or text segment in the application memory is basically the machine code or instructions copied from the executable file. Instructions are not executed directly from secondary storage, they are first copied to main memory, and then they can only be executed during program execution.
We need memory, not only. to store instructions that will be executed, but also to store a large amount of data that we would work with in a program. These other segments are primarily concerned with storing and managing data. What I'm going to do now is zoom in on the code or text segment. Let's assume that each machine language instruction occupies four bytes. I'm trying to show the section of memory that stores the instructions here. Each partition here is a four-byte block and the actress increases from top to bottom, so we have instruction 01 at address 200 and the next instruction is at address 204 and the next one. is at 208 and so on.
The instructions of a program are executed sequentially. The only exception will be when the instruction itself says "hey, go or jump to this other instruction at this particular address", which will happen in the case of function calls, for example if instruction 01 at address 200 is being executed currently by default the next instruction to execute is instruction-02 at address 204, unless instruction 01 itself is something like go to instruction-05 at address 216, which will happen in the case of function calls. A function is nothing more than a set of instructions stored in a contiguous block of memory. Let's say this block containing instructions 5 to 8 is a function.
I will call this function func1. Basically, a function is a set of instructions to perform a subtask. In memory, a function will be a contiguous block with some instructions. The address of a function, which we also call the entry point of a function, will be the address of the first statement of the function. In this example, the address of func1 is 216, a function call in machine language will basically be an instruction to jump to the entry point of some function to jump to the first instruction of a function. We will not delve further into the architecture now, this is enough.
To understand function pointers, when we say that function pointers store the address of functions, we basically mean that function pointers store the starting address or entry point of the block of memory that contains all the instructions in a function . Let us now see how function pointers can be created and used in a C or C plus plus program. I'm going to write a simple C program here. The first thing I'm going to do is write a function called Add that will take two integers as an argument and return the sum. of these two integers and now I'm going to create a function pointer that should point to this Add function.
The syntax for creating a function pointer is to first write the return type of the function that this pointer will point to. Add will return int. So, I wrote int, then after the space in parentheses, I wrote an asterisk and then the name of the variable, so I am creating a pointer called 'p' and now, once again, in parentheses I wrote all the argument types of the function to which this pointer. The types of arguments that will point in the function declaration must be exactly the same as in the function that this pointer will point to because both arguments in Add Function are Int, we also have two integers in the function pointer declaration.
To initialize the function pointer and fill in the address of a function, we can use a statement like this. As we know, business operator gives us the address of something, this statement p = &Add will give us the address of Add in 'p', so 'p' now points to Add and using 'p', we can execute this Add function . What I'm going to do here is declare an integer variable called 'c' and now I'm going to write a declaration like this. What I did here is first use the asterisk operator to dereference and get the function and then passed arguments just as I would with a function name.
So, I passed two integers: two and three, and if all is well, the output of this printf statement here should have an integer value 5. When I run the program, this is what I get. So this is really cool. We simply use a function pointer to refer to a function and then execute the function. One thing about the function pointer declaration syntax. We are putting the identifier or name of the pointer inside this parentheses, if we would not use them then it will mean that we will declare a function that will return the pointer to an integer in this case.
If you wrote something like this, you would write a statement like this, so this would declare a function that would return a pointer to an integer. We can have an asterisk just before the function name or we can have an asterisk just after int. These 2 syntaxes are the same, but if I put this in parentheses, then a function pointer is declared. Okay, a few more things. In this initialization here, we are using this business operator, even if we don't use this business operator, it will mean the same. Simply using the function name will also return us the address of the function or, in other words, an appropriate pointer.
To dereference, instead of using this parenthesis and the asterisk operator with the name of the function pointer, we can simply use the name of the function pointer and this is enough. So we can use the name or identifier of the function pointer just as the function name, as you can see the result here is as expected, so we have two possible syntaxes for referencing and dereferencing; You can use whatever you want. The second is more famous. One last thing, in order to point to a function type, the function pointer has to be appropriate, so in this declaration of 'p' here, if I were to change it to something like this, then it means this function pointer. to point to a function that should take two integers as an argument and should return void.
The Add function returns int, so 'p' cannot point to Add. This will give me a compile error. Again, if I change the declaration of something like this, if I only have one argument in the declaration of a function pointer, then P cannot point to Add. Ok, now let's use some more functions with different signatures and try to execute them using function pointers. I am writing this PrintHello function, it will simply print "Hello" to the screen. We'll need to declare a function pointer like this. and then we can initialize the pointer and fill in the address of this function and now we can execute the function through this pointer.
Let's see what the result is. this looks good. Now let's say we want to say "Hello" to someone and I'll change the signature of PrintHello. To take a string as an argument, the declaration of the function pointer will also change. and while calling we will have to pass a string when running this program, this is what I get and this also looks good. This is how we can create and use function pointers in C or C plus plus. Function pointers are used in a program in interesting scenarios, they have interesting use cases. We will talk about use cases in the next lesson.
That's all for this lesson. Thanks for watching!

If you have any copyright issue, please Contact