From 072953c69ae21a76ba2ce8cd27f12c780c5c1100 Mon Sep 17 00:00:00 2001 From: Kirill Bryntsev Date: Tue, 12 Sep 2023 22:30:27 +0700 Subject: [PATCH] Add information about function pointer (#4460) --- .../content/105-pointers-and-references/index.md | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/data/roadmaps/cpp/content/105-pointers-and-references/index.md b/src/data/roadmaps/cpp/content/105-pointers-and-references/index.md index b5ac5bf6f..c7f205ce1 100644 --- a/src/data/roadmaps/cpp/content/105-pointers-and-references/index.md +++ b/src/data/roadmaps/cpp/content/105-pointers-and-references/index.md @@ -19,6 +19,20 @@ int *ptr = # // Pointer 'ptr' now points to the memory address of 'num' int value = *ptr; // Value now contains the value of the variable that 'ptr' points to (i.e., 10) ``` +**Function pointer:** + +```cpp +int add(int a, int b) +{ + return a + b; +} + +int main() +{ int (*funcptr) (int, int) = add; // Pointer 'funcptr' now points to the functions 'add' + funcptr(4, 5); // Return 9 +} +``` + ## References A reference is an alias for an existing variable, meaning it's a different name for the same memory location. Unlike pointers, references cannot be null, and they must be initialized when they are declared. Once a reference is initialized, it cannot be changed to refer to another variable. @@ -38,4 +52,4 @@ int &ref = num; // Reference 'ref' is now an alias of 'num' Modifying the value of `ref` will also modify the value of `num` because they share the same memory location. -**Note:** References are generally used when you want to pass a variable by reference in function arguments or when you want to create an alias for a variable without the need for pointer syntax. \ No newline at end of file +**Note:** References are generally used when you want to pass a variable by reference in function arguments or when you want to create an alias for a variable without the need for pointer syntax.