-
Notifications
You must be signed in to change notification settings - Fork 15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Pointer function arguments #89
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,15 +1,91 @@ | ||||||
/* | ||||||
------------------------------------------------------------------------------------ | ||||||
Tutorial: brief description of tutorial content | ||||||
Tutorial: This tutorial explains the concept of function pointers as argument in C. | ||||||
|
||||||
We cannot pass a function as an argument to another function, but we can pass the | ||||||
reference of a function as a parameter by using a function pointer. This process is | ||||||
known as call by reference. | ||||||
|
||||||
C programming allows you to create a pointer pointing to the function, which can be | ||||||
further passed as an argument to the function. We can create a function pointer as follows: | ||||||
|
||||||
(return_type) (*point_name) (parameter1, parameter2, ...); | ||||||
|
||||||
In the above syntax: | ||||||
-> return_type: the variable type which is returned by the function | ||||||
-> *pointer_name: the function pointer | ||||||
-> parameter1, parameter2, ... : the list of parameters passed to the function | ||||||
|
||||||
e.g: | ||||||
float (*add)(); this is a valid declaration for the function pointer | ||||||
float *add(); this is an illegal declaration for the funciton pointer | ||||||
|
||||||
A function pointer can also point to another function, or we can say that it holds | ||||||
the address of another function. | ||||||
|
||||||
We can then pass the function pointer as a parameter: | ||||||
|
||||||
1. We need a function F1 that takes as a parameter a function pointer of type TYPE1. | ||||||
2. We need a function of type TYPE1, meaning it has the same return type and same type | ||||||
of arguments in order. Let's call this function Function2 | ||||||
3. We declare a pointer function of type TYPE1. Let's call it FunctionPointer. | ||||||
4. We assign Function2 to FunctionPointer: FunctionPointer = Function2 | ||||||
5. We then call function F1 with FunctionPointer as a parameter. | ||||||
|
||||||
We shall see an illustration of this in the example below. | ||||||
------------------------------------------------------------------------------------ | ||||||
*/ | ||||||
|
||||||
// Code here explaining concept with comments to guide | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
#include <stdio.h> | ||||||
|
||||||
// function declaration | ||||||
int add(int a, int b) | ||||||
{ | ||||||
return a + b; | ||||||
} | ||||||
|
||||||
void f(int x) | ||||||
{ | ||||||
printf("%d\n", x); | ||||||
} | ||||||
|
||||||
// another function declaration | ||||||
void display(void (*p)(int)) | ||||||
{ | ||||||
for (int i=1; i <= 5; i++) | ||||||
{ | ||||||
p(i); | ||||||
} | ||||||
} | ||||||
|
||||||
int main() | ||||||
{ | ||||||
/* declaration of a pointer to a function that returns a integer and takes as arguments | ||||||
2 integers */ | ||||||
int (*a)(int, int); | ||||||
// assigning address of add() to the function pointer "a" | ||||||
a = add; // Now, 'a' is a pointer pointing to the add() | ||||||
|
||||||
// We can call the add() function by using the pointer, i.e., 'a'. | ||||||
printf("%d", a(2, 3)); // prints add(2,3) = 5.0 | ||||||
|
||||||
|
||||||
// How we can pass a function pointer as parameter | ||||||
|
||||||
void (*p)(int); // void function pointer declaration | ||||||
p = f; | ||||||
printf("The values are :"); | ||||||
display(p); // prints 1, 2, 3, 4, 5 each in a new line | ||||||
return 0; | ||||||
} | ||||||
|
||||||
/* | ||||||
------------------------------------------------------------------------------------ | ||||||
Challenge: list challenges to be completed here. minimum of one challenge per tutorial | ||||||
Challenge: | ||||||
1. Write a function that prints "I love pointers!" | ||||||
2. Declare a corresponding function pointer and assign the previous function to it. | ||||||
3. Write a second function with the correspoing function pointer type as parameter. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
4. Call the second function with the first function as a parameter. | ||||||
------------------------------------------------------------------------------------ | ||||||
*/ |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.